iOS UITableViewCell重用问题
2015-02-28 15:11
393 查看
iOS UITableViewCell重用问题
发表:2013-09-14 01:18:30在写微博界面的过程中使用到了cell,那么就是在cell上添加一些控件,但是由于每条微博的内容都是不同的,所以在显示的过程中,出现了内容重叠的问题,其实就是UITableViewCell重用机制的问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @ "Cell" ; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } return cell; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @ "Cell" ; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } return cell; } |
解决方法一:对在cell中添加的控件设置tag的方法
例如在微博内容中需要添加label,那么就可以对添加的label设置tag,然后新建cell的时候先remove前一个cell tag相同的label,再添加新的label,这样就不会出现cell内容的重叠。
1 2 | [[cell viewWithTag: 100 ] removeFromSuperview]; [[cell contentView] addSubview:contentLabel]; |
在实现微博界面中,一个cell会有多个控件(label,imageview...),按理说,对每一个控件都设置tag,按照第一种解决方法,应该是可以实现的。但是在实际运行过程中发现不行,还是会出现内容重叠的问题,所以采用第二种解决方法--在新建cell的时候,如果不是空就删除所有的子视图。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *iddifity=@ "mycell" ; UserGridTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:iddifity]; if (cell==nil) { cell=[[[UserGridTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iddifity]autorelease]; } else { while ([cell.contentView.subviews lastObject] != nil) { [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; //删除并进行重新分配 } } cell.dataArrays=[self.dataArray objectAtIndex:indexPath.row]; return cell; } |
重用机制是根据相同的标识符来重用cell的,标识符不同的cell不能彼此重用。于是我们将每个cell的标识符都设置为不同,就可以避免cell重用问题了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@ "Cell%d%d" , [indexPath section], [indexPath row]]; //以indexPath来唯一确定cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //...其他代码 } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@ "Cell%d%d" , [indexPath section], [indexPath row]]; //以indexPath来唯一确定cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //...其他代码 } |
相关文章推荐
- ios 用xib自定义UITableViewCell的注意事项——重用问题
- iOS中UITableViewCell的重用问题解决方案
- iOS UITableViewCell重用问题
- iOS UITableViewCell重用机制理解之初级剑侠篇(欢迎提建议和分享遇到的问题)
- iOS中UITableViewCell的重用问题解决方案
- iOS UITableViewCell的重用机制出现问题解决
- iOS UITableViewCell的混乱显示与重用问题
- iOS UITableViewCell重用问题
- iOS UITableViewCell重用问题
- iOS开发UI篇—UITableviewcell的性能问题
- UITableViewCell的重用问题
- iOS- UITableViewCell对象是怎么重用的 ?
- UITableViewCell 重用 cell错乱问题
- IOS学习-UITableViewCell的重用机制原理
- iOS tableView 的 cell上其它控件重用(利用view的tag属性巧妙实现重用,从xib中加在cell重载问题)
- iOS之 Grouped类型UITableViewCell的一点问题
- 关于有控件变化的UITableViewCell的重用问题
- UITableViewCell 多种并存以及重用遇到的小问题
- ios之清除cell缓存,解决cell的重用问题。
- iOS开发UI篇—UITableviewcell的性能问题