【问题标题】:dequeueReusableCellWithIdentifier:identifier not picking up a loadNibNamed celldequeueReusableCellWithIdentifier:标识符未拾取 loadNibNamed 单元格
【发布时间】:2010-12-16 18:57:51
【问题描述】:

我有一个简单的单元格——在 IB 中设计——并设置了重用标识符。下面的代码工作得很好。但是 - NSLog() 显示结果永远不会被缓存。

表格视图控制器类:

 - (UITableViewCell *)tableView:(UITableView *)tableView 
          cellForRowAtIndexPath:(NSIndexPath *)indexPath 
 {
    switch/case for various cell types
    {
      Foo * item = [results objectAtIndex:indexPath.row];
      return [MyCell tableView:tableView populatedCellWith:item];
    }
 }

MyCell 类..

+(UITableViewCell *)tableView:(UITableView *)tableView populatedCellWith:(Foo *)item  
{
   static NSString * identifier = @"XXX";

   MyCell *cell = (MyCell *) [tableView dequeueReusableCellWithIdentifier:identifier];
   if (cell == nil) {
       NSArray * items = [[NSBundle mainBundle] loadNibNamed:@"MyCell" 
                                 owner:self options:nil];
       cell = [items objectAtIndex:0];

       assert( cell && [cell.reuseIdentifier isEqualToString:identifier]);

       NSLog(@"That was a load - rather than a nice cache for %@", self.class);
   }
   fill out some stuff.
   return cell;

}

为什么会这样 - 因为它使事情变得更有效率?

谢谢,

Dw.

【问题讨论】:

  • 所以您不止一次看到 NSLog?只是想澄清你的问题。
  • 确实是的 - 并且它们已经设置了一个单元格标识符(这是“断言”检查的内容)。

标签: iphone ipad ios uitableview


【解决方案1】:

您创建表格视图单元格的方式不能确保将单元格放入表格视图中的可重用队列中。唯一的方法是使用

initWithStyle:reuseIdentifier:

Initializes a table cell with a style and a reuse identifier and returns it to the caller.

我的另一个question

【讨论】:

    【解决方案2】:

    您确定单元格已设置单元格标识符吗? UITableView 不会缓存那些没有的。

    【讨论】:

    • 必须在xib中设置,否则assert会失败?
    • 正确 - 这就是断言确实确认的设置。做一些愚蠢的事情(比如假设一个指针比较)并链接 ot 以在下次获取该标识符指针而不是字符串值(例如,使用标识符 = cell.reuseIndentifier)并没有帮助。
    • 哎呀,我错过了assert 行,抱歉。 UITV 可以出于多种原因选择不缓存,但通常归结为缺少重用标识符或周围没有足够的单元格(即没有足够的行需要缓存)。如果两者都不适用,那我就束手无策了。
    • 好的 - 断言确认重用标识符已正确设置。即使在 40 多行中,它也不会缓存。
    猜你喜欢
    • 2013-01-05
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多