美文网首页ios开发
iOS设置不同行cell之间不重用,相同行重用

iOS设置不同行cell之间不重用,相同行重用

作者: mws100 | 来源:发表于2016-07-04 10:34 被阅读1172次

项目中比较复杂的cell重用时出现问题,我的每个cell都有下载进度条,cell重用时进度条都在显示进度。因为下载这里比较复杂,都在cell中完成的处理,所有就想 不同行之间cell能不能不重用。实践了几次,思路是给cell设置不同的reuseIdentifier,实现代码如下:

1)如果是代码创建的cell

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

NSString*cellID = [NSStringstringWithFormat:@"%@%zd",KMyCellID, indexPath.row];

UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:cellID];

if(cell ==nil) {

cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellID];

}

NSLog(@"%zd %@", indexPath.row, cell);

//其余代码......

returncell;

}

2)如果是xib创建的cell

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

NSString*cellID = [NSString stringWithFormat:@"%@%zd", KMyCellID, indexPath.row];

MyTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if(cell ==nil) {

cell = [[[NSBundlemainBundle]loadNibNamed:NSStringFromClass([MyTableViewCellclass])owner:niloptions:nil]lastObject];

//因为reuseIdentifier属性是readonly的,使用kvc赋值

[cell setValue:cellID forKey:@"reuseIdentifier"];

}

NSLog(@"%zd %@", indexPath.row, cell);

//其余代码......

returncell;

}

在打印出来的cell内存地址可以看到,不同行cell的内存地址不一样,相同行cell的内存地址是一样的。

需要注意的是,xib创建的cell设置reuseIdentifier时,因为reuseIdentifier是readonly的,需要使用kvc赋值。

大家有好的实现思路或疑问可留言谈论。

相关文章

网友评论

  • fa9ac582dbcc:没看明白 能加个QQ详细聊下么 353582753

本文标题:iOS设置不同行cell之间不重用,相同行重用

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