2014-12-13 88 views
0

我尝试了许多不同的方式在我的tableview中定制单元格,但没有任何工作。我也尝试过有一个UITableViewCell的子类,但它也没有工作。dequeueReusableCellWithIdentifier:引发异常

所以,我回到了一个更简单的解决方案。

我已在TableView中内部的原型细胞 - 这是被类的UITableViewCell的具有标识符:LocalPostsCell

我的代码:

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

UITableViewCell *cell = [self.postsTable dequeueReusableCellWithIdentifier:@"LocalPostsCell" ]; 

// ... blah blah get values for name/text variables 


     UILabel *nameLabel = (UILabel *)[cell viewWithTag:100]; 

     UILabel *descriptionLabel = (UILabel *)[cell viewWithTag:101]; 

     if (name == nil || [name isEqual:[NSNull null]]) { 
      // handle the place not being available 
      nameLabel.text= @""; 
     } else { 
      nameLabel.text = name; 
     } 

     if (text == nil || [text isEqual:[NSNull null]]) { 
      // handle the place not being available 
      descriptionLabel.text = @""; 
     } else { 
      descriptionLabel.text = text; 
     } 
return cell; 
} 

我还试图这样:

UITableViewCell *cell = [self.postsTable dequeueReusableCellWithIdentifier:@"LocalPostsCell" forIndexPath:indexPath]; 

所有这些都在此行失败,除了(lldb) - 线程1 - 断点1.1错误外,控制台中没有任何内容被打印。

我为所有异常添加了断点,并启用了僵尸功能。

回答

0

您是否在Interface Builder中正确设置了标识符字段(应该是LocalPostsCell)?

另一种可能性是您正在使用不同的表格。通常,使用协议方法中的tableView变量,而不是像self.postsTable那样的自己的引用。

你也应该阅读你的错误信息,看看有什么问题。您可以通过按下调试器中的继续按钮来获得更多信息。

顺便说一句,你可以隐含检查nil,这样你可以更简洁。

nameLabel.text  = name ? name : @""; 
descriptionLabel.text = text ? text : @""; 
0

您是否注册了CellId的笔尖?但是,您不会第一次分配任何东西,请执行以下操作:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LocalPostsCell" ]; 
if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc]init]; 
} 
// Other things... 

return cell;