2010-12-16 77 views
1

我有一个简单的单元格 - 在IB中设计并使用reuseIdentifier集合。下面的代码很好地工作。无论如何 - NSLog()显示结果永远不会被缓存。dequeueReusableCellWithIdentifier:标识符没有选择一个loadNibNamed单元格

表视图控制器类:

- (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。

+0

所以,你所看到的NSLog的不止一次?试图澄清你的问题。 – 2010-12-16 19:11:38

+0

是的确 - 它们已经设置了一个单元格标识符(这是'assert'检查的内容)。 – 2010-12-17 18:04:32

回答

0

您创建表格视图单元格的方式无法确保将单元格放入表格视图中的可重用队列中。只有做到这一点的方法是使用

initWithStyle:reuseIdentifier: 

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

我的另一question

0

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

+0

它必须在xib中设置,否则'assert'会失败? – deanWombourne 2010-12-16 19:41:08

+0

正确 - 这就是断言的确定。做一些愚蠢的事情(比如假设一个指针比较)并且在下一次使用标识符指针而不是字符串值时(例如使用identifier = cell.reuseIndentifier)没有帮助。 – 2010-12-17 18:05:40

+0

哎呀,我错过了'assert'行,对不起。由于各种原因,UITV可以选择不缓存,但通常他们归结为缺少重用标识符或者没有足够的单元(即没有足够的行需要缓存)。如果两者都不适用,我很茫然。 – millenomi 2010-12-19 10:47:39

相关问题