iOS UITableView(十) tableViewcell内容重复的解决办法
2016-01-07 23:04
447 查看
我们都知道tableView是有重用机制的,它的 tableView的实现并且不是为每个数据项创建一个tableCell。而是只创建屏幕可显示最大个数的cell,然后重复使用这些cell,对cell做单独的显示配置,来达到既不影响显示效果,又能充分节约内容的目的。
但是需要注意的是正是因为这样的原因:配置Cell的时候一定要注意,对取出的重用的cell做重新赋值,不要遗留老数据。
为了解决cell复用时带有之前的数据问题我下面列出3个方法供大家参考:
1.重用机制调用的就是dequeueReusableCellWithIdentifier这个方法,方法的意思就是“出列可重用的cell”,因而只要将它换为cellForRowAtIndexPath(只从要更新的cell的那一行取出cell),就可以不使用重用机制,因而问题就可以得到解决,但会浪费一些空间
2.为每个cell指定不同的重用标识符(reuseIdentifier)来解决。重用机制是根据相同的标识符来重用cell的,标识符不同的cell不能彼此重用。
3.删除重用cell的所有子视图得到一个没有特殊格式的cell,供其他cell重用。
但是需要注意的是正是因为这样的原因:配置Cell的时候一定要注意,对取出的重用的cell做重新赋值,不要遗留老数据。
为了解决cell复用时带有之前的数据问题我下面列出3个方法供大家参考:
1.重用机制调用的就是dequeueReusableCellWithIdentifier这个方法,方法的意思就是“出列可重用的cell”,因而只要将它换为cellForRowAtIndexPath(只从要更新的cell的那一行取出cell),就可以不使用重用机制,因而问题就可以得到解决,但会浪费一些空间
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改为以下的方法 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行,而不是从cell重用队列中取出 if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //其他代码 return cell; }
2.为每个cell指定不同的重用标识符(reuseIdentifier)来解决。重用机制是根据相同的标识符来重用cell的,标识符不同的cell不能彼此重用。
<span style="font-size:14px;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@"CellIdentifier%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //列出可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //其他代码 return cell; }</span>
3.删除重用cell的所有子视图得到一个没有特殊格式的cell,供其他cell重用。
<span style="font-size:14px;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //列出可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } else { //删除cell的所有子视图 while ([cell.contentView.subviews lastObject] != nil) { [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; } } //其他代码 return cell; }</span>
相关文章推荐
- iOS字体 UIFont 字体名字大全
- break和continue
- 课后族项目实战(2)-新特性的实现(使用UICollectionView)
- android 事件处理机制之requestDisallowInterceptTouchEvent
- Android Studio——为什么说android UI操作不是线程安全的
- 用runtime 解决UIButton 重复点击问题
- UITextAttributeTextColor 的替换方法+自定义导航控制器的价值
- RESideMeue使用观后感
- 关于UIImagePickerController
- druid配置
- iOS开发 UI--动画
- String baseprice = request.getParameter("baseprice")==null相关问题
- UE4 动画系统
- UE4 动画系统
- UIPopoverController
- iOS UI-团购案例(通过xib文件自定义UITableViewCell)
- iOS开发UI篇—懒加载
- UE4 创建第三人称角色
- UE4 创建第三人称角色
- Upgrade bank is empty or corrupted for FPC 0, please do standard upgrade sequence 错误解决&juniper 交换机系统升级