iphone – 当我初始化自定义单元格时,它没有设置为'[(超级或
发布时间:2020-12-14 19:52:48  所属栏目:百科  来源:网络整理 
            导读:在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;} 在
                
                
                
            | 
 在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;} 一切都按照我的预期工作,但是当我做了产品 – >分析我得到 解决方法
 您正在使用从数组返回的对象覆盖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 *表示将返回保留的对象. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
