gpt4 book ai didi

iphone - 当我初始化自定义单元格时返回 'self' 而它没有设置为 '[(super or self) init...]' 的结果

转载 作者:可可西里 更新时间:2023-11-01 03:37:20 25 4
gpt4 key购买 nike

在 CustomCell.m 中,我定义了我想从 IB 加载单元格的 init 方法:

- (id)init {
self = [super init];
if (self) {
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
self = [nib objectAtIndex:0];

}
return self;
}

在 MyTableViewController.m 的 cellForRowAtIndexPath 方法中,我初始化了我的自定义单元格

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

cell=[[CustomCell alloc]init];
return cell;

一切都如我所料,但当我执行 Product -> Analyse 时,我得到了
在未设置为“[(super or self) init...]”的结果时返回“self”
我做错了什么?

最佳答案

您正在用数组返回的对象覆盖 self(从 super init 返回)。如果您想从 nib 加载自定义单元格,请在您的 cellForRowAtIndexPath 方法中执行,或者在您的自定义单元格上创建一个从 nib 加载的便捷类方法:

在您的 cellForRowAtIndexPath 中:

cell = [CustomCell cell];

在您的单元格实现中:

+(CustomCell*)cell
{
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
return [nib objectAtIndex:0];
}

编辑 - 更改了方法名称,因为 new* 表示将返回保留的对象。

关于iphone - 当我初始化自定义单元格时返回 'self' 而它没有设置为 '[(super or self) init...]' 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8909138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com