xib创建Cell时重用数据混乱问题解决方案
2017-04-12 17:55
330 查看
写这篇文章是因为在项目中遇到了这个问题,,所以拿下来和大家一起分享,平常一直没有因为复用问题而导致数据复用混乱,
先看看效果图:
出现了旧的数据,所以现在这个问题就不能在使用registerNib注册xib方法了,一般复用出现数据混乱可能原因就是cell中包含UITextField和
UICollectionView类型的数据时,出现数据混乱的情况比较大,这时候我们就需要做一些特别得操作,
思路:1:每次拿到cell进行判断是否为nil
2:为nil就重新创建
3:不为nil时就删除所有的子视图
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HistoryModel * model = self.CellModelArr[indexPath.row];
// historyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"historyCell" forIndexPath:indexPath];
historyCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//解决xib复用数据混乱问题
if (nil == cell) {
cell= (historyCell *)[[[NSBundle mainBundle] loadNibNamed:@"historyCell" owner:self options:nil] lastObject];
}else{
//删除cell的所有子视图
while ([cell.contentView.subviews lastObject] != nil)
{
[(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
}
}
// historyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"historyCell"];
// if (cell == nil) {
// cell = [[historyCell alloc] init];
// }
cell.IsBuy.hidden = YES;
cell.hisModel =model;
return cell;
}
最后运行就发现这种解决方法是没毛病
先看看效果图:
出现了旧的数据,所以现在这个问题就不能在使用registerNib注册xib方法了,一般复用出现数据混乱可能原因就是cell中包含UITextField和
UICollectionView类型的数据时,出现数据混乱的情况比较大,这时候我们就需要做一些特别得操作,
思路:1:每次拿到cell进行判断是否为nil
2:为nil就重新创建
3:不为nil时就删除所有的子视图
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HistoryModel * model = self.CellModelArr[indexPath.row];
// historyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"historyCell" forIndexPath:indexPath];
historyCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//解决xib复用数据混乱问题
if (nil == cell) {
cell= (historyCell *)[[[NSBundle mainBundle] loadNibNamed:@"historyCell" owner:self options:nil] lastObject];
}else{
//删除cell的所有子视图
while ([cell.contentView.subviews lastObject] != nil)
{
[(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
}
}
// historyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"historyCell"];
// if (cell == nil) {
// cell = [[historyCell alloc] init];
// }
cell.IsBuy.hidden = YES;
cell.hisModel =model;
return cell;
}
最后运行就发现这种解决方法是没毛病
相关文章推荐
- 利用NSOperation进行异步图片下载——设置UITabView数据,图片下载,占位图。解决异步下载和Cell重用造成的图片设置混乱。和多次重复下载的问题
- 解决在listview中重用viewholder导致数据混乱的问题
- TableView的cell加载倒计时重用问题解决方案
- Swift cell滑动数据丢失,图片重叠,cell重用问题的解决
- UITableview 多选 解决cell重用问题
- 加密解密时遇到的"不正确的数据"以及"要解密的数据长度无效"问题解决方案
- WCF+Restfull服务 提交或获取数据时数据大小限制问题解决方案
- 解决tableView中cell动态加载控件的重用问题
- web应用中webapp. root重用问题解决方案
- ios之清除cell缓存,解决cell的重用问题。
- WCF+Restfull服务 提交或获取数据时数据大小限制问题解决方案
- 动态创建的 HTML 控件提交后数据丢失问题及解决
- 使用NEWSEQUENTIALID解决GUID聚集索引问题 SQLServer数据表主键的另一个方案
- 关于ListVIew加载数据混乱的问题分析和解决
- 加密解密时遇到的"不正确的数据"以及"要解密的数据长度无效"问题解决方案
- 初学Flex,在使用Webservice时遇到Xml数据绑定的一个问题,试了N个方案,均没解决。
- Sql Server 2005 与Sql Server Mobile(Sql server 2005 mobile Edition)数据同步步骤以及问题解决方案
- maven创建web工程Spring配置文件找不到问题解决方案
- iOS中UITableViewCell的重用问题解决方案
- Excel数据导入ArcGIS中出现的投影问题解决方案