美文网首页
unable to dequeue a cell with id

unable to dequeue a cell with id

作者: zfl1024 | 来源:发表于2017-11-24 10:15 被阅读0次

iOS6新增了这个方法:

dequeueReusableCellWithIdentifier:forIndexPath:

而以前是这个:

dequeueReusableCellWithIdentifier:

在此之前cell的重用写法是:

static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; 
}

而之后多了这种写法:

static NSString *ID = @"cell";
[self.tableView registerClass:[MyCell class] forCellReuseIdentifier:ID];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];

区别在于之前的写法取出重用cell的时候可能是空的,而后来的写法如果取出空的那就自动创建一个新的 ,registerClass方法就是告诉tableView创建个什么样的。

相关文章

网友评论

      本文标题:unable to dequeue a cell with id

      本文链接:https://www.haomeiwen.com/subject/vqexbxtx.html