繁体   English   中英

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

[英]Returning 'self' while it is not set to the result of '[(super or self) init…]' when I initialize custom cell

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

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

    }
    return self;
}

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

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

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

}

一切都按照我的预期工作,但当我做Product -> Analyse我得到
Returning 'self' while it is not set to the result of '[(super or self) init...]'
我究竟做错了什么?

您正在使用从数组返回的对象覆盖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 *表示将返回保留的对象。

保持您的init方法如下,并在Interface Builder中进行链接

- (id)init {
    self = [super init];
    if (self) {

    }
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }
}

我遇到了同样的问题,我通过删除喜欢的代码修复了它

NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];         
return [nib objectAtIndex:0];

来自CustomView的定义init方法。

将这些代码放在您创建自定义的位置。

我在做什么

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {
        // Initialization code.
        //
        UITableViewCell *view = [[[NSBundle mainBundle] loadNibNamed:@"SmallCellView" owner:self options:nil] lastObject];
        self.backgroundView = view;
}
    return self;
}

然后在主类

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    SmallCellView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[SmallCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
 }
  return cell;
}

对我来说,这工作正常,在Product -> Analyse中没有给出任何警告或错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM