插座无法连接到重复内容 iOS 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27142581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:48:11  来源:igfitidea点击:

Outlets cannot be connected to repeating content iOS 5

iosuitableview

提问by Mishal Awan

I am new to iOS i am working on uitableview cell i had drag and drop the button to table view cell from objects inspector. but when i am making its IBoutlet it is showing an error like "Outlets cannot be connected to repeating content" what does it means?? we cant make outlets of UIbutton in tableview cell.kindly review it . i am stuck in it.i am making an outlet like this:

我是 iOS 新手,我正在处理 uitableview 单元格,我已将按钮从对象检查器拖放到表格视图单元格。但是当我制作它的 IBoutlet 时,它显示了一个错误,比如“Outlets 无法连接到重复的内容”这是什么意思?我们不能在 tableview 单元格中制作 UIbutton 的出口。请检查一下。我被困在里面。我正在做一个这样的出口:

@property (weak, nonatomic) IBOutlet UIButton *sa;

and the error is "The sa outlet to the UIbutton is invalid"

错误是“UIbutton 的 sa 插座无效”

回答by Vishal Sharma

Your answer is that you use an object in UITableViewCell by reusing it,

您的答案是您通过重用 UITableViewCell 中的对象,

So you can't create it's outlet except you create Your class for your "UITableViewCell".

所以你不能创建它的插座,除非你为你的“UITableViewCell”创建你的类。

Below is a reference for you,

下面给大家做个参考

I hope that it's useful for you.

我希望它对你有用。

You need to drag and drop the object in your UITableViewCell, Then you have to give tag for that object.

您需要将对象拖放到您的 中UITableViewCell,然后您必须为该对象提供标签。

then you can use it with its tag.

然后你可以使用它的标签。

First give an identifier to UITableViewCell,

首先给一个标识符UITableViewCell

Below is a reference image for it.

下面是它的参考图像。

enter image description here

在此处输入图片说明

Then give a tag to your UI object, As this in this reference image.

然后给你的 UI 对象一个标签,在这个参考图像中。

enter image description here

在此处输入图片说明

Below is sample code for you which I use regularly,

以下是我经常使用的示例代码,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    UITableViewCell *Cell = [self.TableListRegion dequeueReusableCellWithIdentifier:@"List"];

    UIButton *objectOfButton = (UIButton *)[CellLast viewWithTag:200];

    [objectOfButton addTarget:self action:@selector(YourSelector:) forControlEvents:UIControlEventTouchUpInside];

    return Cell;

}

Now you can receive that button event by,

现在您可以通过以下方式接收该按钮事件,

-(IBACTION)YourSelector:(id)sender{
// Your Button in Cell is selected.
// Do your stuff.

}

Feel free to ask if you need more help regarding it.

随时询问您是否需要更多帮助。

回答by ChintaN -Maddy- Ramani

You can create subclass for UITableViewCell like below code

您可以像下面的代码一样为 UITableViewCell 创建子类

Create new class named CCell.h and CCell.m

创建名为 CCell.h 和 CCell.m 的新类

in CCell.h

CCell.h

@interface CCell : UITableViewCell
@property(nonatomic,strong)IBOutlet UILabel *lblTemp;
@property(nonatomic,strong)IBOutlet UIButton *btnTemp;
@end

Now in your Viewcontroller.h

现在在你的Viewcontroller.h

#import "CCell.h"

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CCell *cell = (CCell *)[tblView dequeueReusableCellWithIdentifier:@"CCell"];
    cell.lblTemp.text = @"asd";

    cell.btnTemp.tag = indexPath.row;
    [cell.btnTemp addTarget:self action:@selector(btnTempClicked:) forControlEvents:UIControlEventTouchUpInside]
    return cell;
}

-(void)btnTempClicked:(UIButton *)btnTemp
{ 
     NSLog(@"Button Clicked Index = %d",btnTemp.tag);   
}

Now open your Xib > tap on your UITableviewCell > open right side navigator > open 3rd tab named (Custom Class) > add Class = CCell> now open last tab you will get lblTempbind option.

现在打开您的 Xib > 点击您的 UITableviewCell > 打开右侧导航器 > 打开名为(自定义类)的第三个选项卡 > 添加类 = CCell> 现在打开最后一个选项卡,您将获得lblTemp绑定选项。

Maybe this will help you.

也许这会帮助你。

回答by Sandy D.

Since you are creating a custom cell, you need to create a class for it. You will subclass UITableViewCell.

由于您正在创建自定义单元格,因此您需要为其创建一个类。您将继承 UITableViewCell。

For example (using the property that you had in your question):

例如(使用您在问题中的属性):

  1. Create a new Objective-C Class. Set the subclass to: UITableViewCell Give it an appropriate name (i.e. cell)

  2. In your cell.h file:

  1. 创建一个新的 Objective-C 类。将子类设置为: UITableViewCell 给它一个合适的名字(即单元格)

  2. 在您的 cell.h 文件中:

Create your property: @property (weak, nonatomic) IBOutlet UIButton *sa;

创建你的属性:@property (weak, nonatomic) IBOutlet UIButton *sa;

In the cell.m file:

在 cell.m 文件中:

@synthesize sa = _sa;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self) {
    // Initialization code
}

return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

3.In Interface Builder, go back to the row that you created. Click the "Show Identity Inspector". Under "Custom Class", set that to your cell file.

3.在Interface Builder中,返回到您创建的行。单击“显示身份检查器”。在“自定义类”下,将其设置为您的单元格文件。

4.Hold down the "Option" key and click the "cell.h" file. Then connect the button to the IBOutlet.

4.按住“Option”键并单击“cell.h”文件。然后将按钮连接到 IBOutlet。

5.In your table view controller file:

5.在你的表视图控制器文件中:

import your cell.h file.

导入您的 cell.h 文件。

In cellForRowAtIndexPathmethod:

cellForRowAtIndexPath方法中:

Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

That's it!

就是这样!

Everything should work then. Hope this helps!

一切都应该工作。希望这可以帮助!

回答by Preetha

I have done in UITableviewcell cell's label's outlet connection in UIViewController,it should changed to Create a CustomCell in Subclass of UITableviewcell,then done in outlet connection in this subclass that error cleared.

我已经在 UIViewController 中 UITableviewcell 单元格的标签的插座连接中完成了,它应该更改为在 UITableviewcell 的子类中创建一个 CustomCell,然后在这个错误清除的子类中的插座连接中完成。

回答by guozqzzu

@Mishal Awan

@米沙尔阿万

If you really want to do that and your have a finite number of cells. U can :

如果您真的想这样做并且您的单元格数量有限。你可以 :

  1. Changing your ViewControllerto be a subclass of UITableViewController, then drage a Table View Controllerfile to your StoryBoard
  2. Changing the content of Table Viewin your StoryBoardfrom Dynamic Prototypesto Static Cells
    1]
  3. Then you can add some views to your cell, for example some labels
    2]

  4. Connectting the labelto your ViewControllerand continue the remaining work

  1. 将您更改ViewController为 的子类UITableViewController,然后将Table View Controller文件拖到您的StoryBoard
  2. 改变Table View你的内容StoryBoardDynamic PrototypesStatic Cells
    1]
  3. 然后你可以向你的单元格添加一些视图,例如一些标签
    2]

  4. 连接label到你的ViewController并继续剩下的工作

If you want to create a reusable cell, forget what i have said above

如果你想创建一个可重复使用的单元格,忘记我上面说的

回答by Alok

A very simple solution is:

一个非常简单的解决方案是:

Just take the view or NSLayoutConstraint reference outlets in the subclass of table view cell instead of table view controller and access using object of table view cell in cellForRowAtIndexPath method or any other method.

只需在表格视图单元的子类中获取视图或 NSLayoutConstraint 引用出口,而不是表格视图控制器,并在 cellForRowAtIndexPath 方法或任何其他方法中使用表格视图单元的对象进行访问。

Note: This is a repetitive object so you can't take reference in table view controller.

注意:这是一个重复的对象,所以你不能在表视图控制器中引用。

回答by Khanh Nguyen

You can't, because the button is part of a cell, and there will (possibly) be multiple instances of that cell when the app runs.

您不能,因为按钮是单元格的一部分,并且在应用程序运行时(可能)会有该单元格的多个实例。

Assume that you can make the connection, and there are 2 cells (and thus 2 buttons) when the app runs, Cocoa Touch can't decide which button will be referenced by that only outlet (neither can you).

假设您可以建立连接,并且在应用程序运行时有 2 个单元格(因此有 2 个按钮),Cocoa Touch 无法决定哪个按钮将被该唯一的插座引用(您也不能)。

You can subclass the UITableViewCell, ask the table view to use your subclass for its cells, and connect the button to the outlet in the subclass. In that case there will be multiple instances of the subclass, and each instance will map to one cell.

您可以将 子类化UITableViewCell,让表视图使用您的子类作为其单元格,并将按钮连接到子类中的插座。在这种情况下,子类将有多个实例,每个实例将映射到一个单元格。

回答by Krishna Kumar

Whenever you need want to create custom tableView cell it is recommended to subclass UITableViewcelland add UIButtonin that class. And in your tableview delegate method cellForRowAtIndexPathyou can create an object of subclassed tableviewCell for every cell.

每当您需要创建自定义 tableView 单元格时,建议子类化UITableViewcell并添加UIButton到该类中。在您的 tableview 委托方法中,cellForRowAtIndexPath您可以为每个单元格创建一个子类 tableviewCell 的对象。