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

项目中比较复杂的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赋值。

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容