UITableView 的选中状态

xiaoxiao2021-02-28  19

一、cell 在初始化或者重用时,调用 -_configureCellForDisplay:forIndexPath: 方法,这个方法会做一些附带操作,其中会调用 setSelected:animated: 方法,将 cell 的选中状态置为未选中。



1 在 tableView:cellForRowAtIndexPath: 中调用 setSelected:animated: 方法将 cell 设置为了选中

。

2 之后又在 -_configureCellForDisplay:forIndexPath: 中调用了setSelected:animated: 方法,将 cell 设置为了未选中

。

3 可以通过实现 UITableViewDelegate 的 willDisplayCell:forRowAtIndexPath: 方法,在 cell 即将显示的时候,对相应 indexPath 的 cell 的选中状态进行设置,这个方法会在 -_configureCellForDisplay:forIndexPath: 后调用。

UITableView 会在再次显示到屏幕上时将已选中的 cell 选中状态置为未选中,若需要保存选中状态则需要自己实现,保存选中的 cell 的 IndexPath,并在 viewWillAppear: 中调用 selectRowAtIndexPath:animated:scrollPosition: 方法将保存的 NSIndexPath 数组中对应的 cell 选中。 

二、参考资料 http://stackoverflow.com/a/25128477 http://stackoverflow.com/a/30736675         https://imciel.com/2016/06/15/select-uitableviewcell-the-right-way/

转载请注明原文地址: https://www.6miu.com/read-2350346.html

最新回复(0)