简体   繁体   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

In CustomCell.m I define init method where I want to load cell from the IB: 在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;
}

In the MyTableViewController.m in the method cellForRowAtIndexPath I initialize my custom cell 在方法cellForRowAtIndexPath中的MyTableViewController.m中,我初始化我的自定义单元格

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

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

} }

Everything works as I expected but when I did Product -> Analyse I get 一切都按照我的预期工作,但当我做Product -> Analyse我得到
Returning 'self' while it is not set to the result of '[(super or self) init...]'
What am I doing wrong? 我究竟做错了什么?

You are overwriting self (returned from super init ) with the object returned from your array. 您正在使用从数组返回的对象覆盖self (从super init返回)。 If you want to load a custom cell from a nib, do it in your cellForRowAtIndexPath method, or create a convenience class method on your custom cell that loads from the nib: 如果要从nib加载自定义单元格,请在cellForRowAtIndexPath方法中执行此操作,或者在从nib加载的自定义单元格上创建一个便捷类方法:

In your cellForRowAtIndexPath: 在你的cellForRowAtIndexPath中:

cell = [CustomCell cell];

In your cell's implementation: 在您的单元格的实现中:

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

EDIT - changed method name since new* indicates that a retained object will be returned. 编辑 - 更改了方法名称,因为new *表示将返回保留的对象。

Keep your init method as below, and do the linking in the Interface Builder 保持您的init方法如下,并在Interface Builder中进行链接

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

    }
    return self;
}

And

- (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;
            }
        }
    }
}

I met the same problem, I have fixed it by removing the code that like 我遇到了同样的问题,我通过删除喜欢的代码修复了它

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

from the CustomView's definition init Method. 来自CustomView的定义init方法。

Put these code the at the place where you create the Custom. 将这些代码放在您创建自定义的位置。

What I am doing is 我在做什么

-(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;
}

then in the main class 然后在主类

- (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;
}

For me this is working fine and in Product -> Analyse is not giving any warning or error 对我来说,这工作正常,在Product -> Analyse中没有给出任何警告或错误

暂无
暂无

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

相关问题 在我的自定义alloc方法中未将其设置为'[((super或self)init…])的结果时返回'self' - Returning 'self' while it is not set to the result of '[(super or self) init…]' in my custom alloc method “self”时使用的实例变量未通过Analyzer设置为“[(super或self)init ...]”的结果 - Instance variable used while 'self' is not set to the result of '[(super or self) init…]' via Analyzer iPhone - 当[super init]失败时使用self = [super init] - iPhone - Use of self = [super init] when [super init] fails if(self = [super init])与if((self = [super init])) - if (self = [super init]) vs. if ((self = [super init])) “self”时使用的实例变量未设置为结果 - Instance variable used while 'self' is not set to the result of 何时在Objective-C的初始化方法中使用self.property,如何在初始化方法中初始化数组 - when to use self.property in init methods in objective-c, how to initialize arrays in init methods if(self = [super init]) - LLVM警告! 你是怎么处理它的? - if(self = [super init]) - LLVM warning! How are you dealing with it? 谁能给我一个关于“ self = super init”的简单解释? - Can anyone give me an easy explanation about “self=super init”? 自我与超级之间的区别 - Difference between self and super 为什么在init中使用DispatchQueue时会在self.init调用之前使用错误“ self”? - Why error ''self' used before self.init call' when using DispatchQueue in init?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM