APP的一个页面用到了自定义的UITableViewCell,由于iOS框架的cell重用机制,遇到了一个BUG,总结一下
现象
自定义的UITableViewCell里有一个UIButton,点击这个button以后,需要改变cell的样式,包括换UILabel字体颜色,禁用该UIButton等。结果发现,点击按钮之后,不仅当前cell的字体颜色变了,还有另外几个cell的字体颜色也跟着变,而且是随机的
原因
后来想到,应该是由于iOS的cell重用机制造成的,原来的代码类似:
-(void) onButtonPressed
{
label.textColor = [UIColor grayColor];
button.enabled = NO;
}
其中label和button都是这个cell的实例变量,由于cell是自动重用的,所以其他重用此cell的格子也会跟着一起变
正确的做法
修改之后,正确的做法应该是:
1、在controller中找到此cell对应的模型
2、修改模型对应的值
3、调用tableView的reloadData方法
4、在dataSource的代理方法里,再调用cell上的设置样式的方法
示意代码:
-(void) voteButtonPressed
{
[myController voteWithCell:self];
}
-(void) voteWithCell:(CandidateTableViewCell*)cell
{
RankingView *myView = (RankingView*)self.view;
// 找到对应的模型
NSIndexPath *indexPath = [myView.tableView indexPathForCell:cell];
Candidate *candidate = [candidates objectAtIndex:indexPath.row];
// 设置新值
candidate.voteCount++;
candidate.isVoted = YES;
// 触发数据加载
[myView.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CandidateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[CandidateTableViewCell reuseIdentifier] forIndexPath:indexPath];
Candidate *candidate = [candidates objectAtIndex:indexPath.row];
[cell setCandidate:candidate isExpired:self.stage != 1 controller:self];
return cell;
}
分享到:
相关推荐
SSDataSources, UITableView和UICollectionView的灵活数据源 SSDataSources 用于 UITableView 和 UICollectionView的灵活数据源。 , 。你肯定已经做了 tableView:cellForRowAtIndexPath: 和 tableView:
Preheat:自动化在UITableView和UICollectionView预取内容
IOS UITableView UIcollectionView 结合
自定义iOS网格组件,基于UITableView 和 UICollectionView 构建的 GridView
ReactiveLists为UITableView和UICollectionView提供了类似React的API,可以轻松编写生成用户界面的无状态代码
用于UITableView&UICollectionView的声明式类型安全管理器
GSKStretchyHeaderView, UITableView和UICollectionView的通用可以伸缩头 GSKStretchyHeaderView,由 gskbyte GSKStretchyHeaderView是在许多应用中看到的可以伸缩头模式的实现,如 Twitter 。Spotify或者
LiveCollections是一个开源框架,只需几行代码就可以使用UITableView和UICollectionView动画
UICollectionViewCell的移动和移动
用于UITableView&UICollectionView的声明式的类型安全管理器。.zip,A declarative type-safe framework for building fast and flexible list with Tables & Collection
用于UITableView和UICollectionView的类似React的API ReactiveLists提供了用于UITableView和UICollectionView的类似于React的API,可轻松编写生成用户界面的无状态代码。 根据我们的经验,这个类似于UITableView和...
Animated top menu for UITableView / UICollectionView / UIScrollView written in Swift.zip,uitableview的动画顶部菜单/uicollectionview/uiscrollview用swift编写
自动预热(预取)UITableView和UICollectionView中的内容。 该库类似于iOS 10中添加的UITableViewDataSourcePrefetching和UICollectionViewDataSourcePrefetching。使用预热的一种方法i自动预热(预取)UITableView...
UITableView 删除 Cell Demo Delete
用于构建基于组件的用户界面的声明性库在UITableView和UICollectionView中。 陈述式 基于组件 无损 提供具有差异算法功能的声明性设计,用于构建列表UI。 一次声明组件,无论列表元素的种类如何,都可以重用。 在...
它旨在满足以下要求: 与UITableView和UICollectionView兼容性数据源和委托独立性:可以添加到现有视图控制器中,而不会干扰您现有的delegate或dataSource 提供对框架布局,自动布局和Interface Builder .xib文件的...
TGRArrayDataSource和TGRFetchedResultsDataSource是实现可重用的类UITableView和UICollectionView使用数据源NSArray和NSFetchedResultsController分别。 受启发。 TGRFetchedResultsTableViewController是一个可...
UICollectionView 与 UITableView 的联动处理 ,更好的理解联动的处理,以及写法
LiveCollections是一个开放源代码框架,它使仅几行代码即可使用UITableView和UICollectionView动画。 给定两组数据,该框架将自动执行所有计算,而bui LiveCollections是一个开放源代码框架,它使仅几行代码即可使用...
UICollectionView替换UITableView。类似Parallax Header,Sticky Section Header。.zip,uiCollectionView替换uiTableView。更像是视差标题,粘性部分标题。专为iOS 7打造。