【问题标题】:cell is nil in prototype cell原型单元格中的单元格为零
【发布时间】:2014-01-11 14:35:23
【问题描述】:

我是 iPhone 开发的新手,我有一个问题,我的应用程序包含从按钮单击到 tableView 的导航(我使用故事板)。我创建了 custum 原型 cell,它的类和标识符。但是在我调试的方法中,它涉及到cell==nil 并抛出异常: 'NSInternalInconsistencyException', reason: '无法在包中加载NIB

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"transportTableCell";
    CNTransportCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSLog(@"size of %d",transportArray.count);
    cell.modelLabelView.text =  @"sometext";

if(cell == nil){

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

}

通过点击导航到表格视图的代码:

 - (IBAction)searchButtonClick:(id)sender {
    CNJSONParser *parser = [[CNJSONParser alloc]init];
    NSString *url = [parser getURL:@"43" :@"66"];

    CNCarListWindowController *cars = [[CNCarListWindowController alloc]init];
    cars.transportArray = [parser parseJSON:url];
    [self.navigationController pushViewController:cars animated:TRUE]; 

}

【问题讨论】:

    标签: ios iphone ios5 uitableview


    【解决方案1】:

    只需如下更改您的代码

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"transportTableCell";
        CNTransportCell *cell;
    
    if(cell == nil){    
       cell = (CNTransportCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }
    return cell;
    

    【讨论】:

    【解决方案2】:

    首先,您使用的是故事板,并且在您使用 xib 时会使用您实现的这种方法。

    在情节提要中,您需要在表格视图的“显示属性检查器”中提供标识符

    【讨论】:

      猜你喜欢
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多