Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从xib加载自定义单元格,但将插座保留在单元格内而不是ViewController?_Ios_Objective C - Fatal编程技术网

Ios 如何从xib加载自定义单元格,但将插座保留在单元格内而不是ViewController?

Ios 如何从xib加载自定义单元格,但将插座保留在单元格内而不是ViewController?,ios,objective-c,Ios,Objective C,直到现在,我一直在做这样的事情: - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code NSArray *

直到现在,我一直在做这样的事情:

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

        NSArray *nibsArray = [[NSBundle mainBundle] loadNibNamed:@"BOAFormCell" owner:self options:nil];
        self = nibsArray[0];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.valueTextLabel.numberOfLines = 2;
    }
    return self;
}
这显然会导致内存泄漏

如果我将加载代码放入cellForRowAtIndexPath中,那么它将使ViewController成为xib的所有者,并且它将在VC中查找出口。

我已经在很多ViewController中重复使用了该单元

我想避免这个漏洞,但我不希望所有这些VC都成为xib的个人所有者,在那个里实施IBActions,并在里面拥有门店的财产

我是否应该制定一个静态方法,从xib加载电池并将其返回给我?
但是,如何使对象成为xib的所有者呢?

您必须创建.xib文件,然后创建一个继承自
UITableViewCell
的类,该类的名称与.xib相同

在.xib中,您将根据需要设计单元。没有ViewController,只有一个UIViewCell及其插座

选择单元格,然后在实用程序/检查器中,将单元格的类别设置为自定义单元格的名称

现在,如果使用“助理编辑器”,您将能够将插座拖放到.h文件中,以向自定义单元格添加属性

在项目的任何类别中,如果导入全新单元格的标题,则可以通过调用以下命令创建自定义单元格:

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
UITableViewCell *cell = [nib objectAtIndex:0];
(在本例中,我的文件名为:CustomCell.h/CustomCell.m/CustomCell.xib)


为该自定义单元格创建的属性可以像任何类的任何属性一样访问。

在Tableview中,您可以为运行自定义单元格添加此代码。我希望这对您有用

 - (UITableViewCell *)tableView:(UITableView *)tableView
            cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        CustomCell *cell =
            [tableView dequeueReusableCellWithIdentifier:@"Cell"
                                            forIndexPath:indexPath];
        if (cell == nil) 
    {
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    UITableViewCell *cell = [nib objectAtIndex:0];

    }
cell.textLabel.text = @"Hi";

return cell;
    }

对于iOS 6+,您应使用以下方法:

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
在加载单元格之前调用它

还有其他类似的方法,您也可以在其中注册页眉和页脚,甚至是通过编程生成的单元,将类注册到tableview

    - (void) viewDidLoad {

        ...
        [self.tableView registerNib:[UINib nibWithNibName:@"CellId" bundle:nil] forCellReuseIdentifier:cellIdentifier];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView
                cellForRowAtIndexPath:(NSIndexPath *)indexPath 
        {
            CustomCell *cell =
                [tableView dequeueReusableCellWithIdentifier:@"CellId"
                                                forIndexPath:indexPath];
      }

dequeueReusableCellWithIdentifier
将始终返回一个单元格,因此无需在代码中检查
cell==nil

,当您说“owner:self”时,您将更改执行代码的对象的所有权。没有办法,这是我问题的答案。我不确定你想做什么。您有一个单元格,并且在多个ViewController中使用它?那么,您希望它的方法和操作是您的单元格的属性,由您的单元格执行,而不是由显示该单元格的VC执行?是的。确切地但是不会[[NSBundle mainBundle]loadNibNamed:@“CustomCell”所有者:self选项:nil];将单元格的所有者更改为我正在编写代码的类?使用UITableCellView为其视图创建UIViewController如何?此ViewController将管理出口和方法。然后,对于其他控制器,创建基本单元,并将此视图添加到其子视图中。因此,自定义单元格的视图显示在您想要的控制器中,但仅由一个控制器管理。我找到了一个解决方案。在interface builder中,我有tableViewCell,我将所有IBoutlet和IBActions连接到作为顶级对象的单元格。因此,无论文件所有者是谁,都会在自定义单元格中查找输出和操作。