UITableView&UICollectionView设置单元格的默认选中状态

1. 场景需求 一个表格视图(或者宫格视图)中,当一个单元格被选中时设置彩色样式,选中其它单元格时设置灰色样式。
2. 一个思路 通过实现选中和非选择的代理,以在适当的时机进行UI更新操作。
3. UITableView 3.1 通过屏幕点击改变的选中状态回调给代理 //选中-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath; //非选中-(void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath;
3.2 代码设置默认选中状态 (要等数据加载完成之后再调用) 执行方法的主体:tableview对象
//选中-(void)selectRowAtIndexPath:(nullable NSIndexPath*)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition; //非选中-(void)deselectRowAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated;
注意的是:
上述代码强制设置某单元格选中或者不选中那一刻,都不会回调tableview的选中代理方法,也不会发出通知UITableViewSelectionDidChangeNotification。
之后,通过屏幕点击选中其它cell的时候,可以执行- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath; 代理方法,你就有机会还原cell的默认样式。
3.3 补充:代码设置默认选中状态 执行方法的主体:cell对象
-(void)setSelected:(BOOL)selected animated:(BOOL)animated; // animate between regular and selected state
注意的是:
这种方法改变cell的选中状态时,当通过屏幕点击选中其它cell的时候,UITableView并不会执行- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath; 的非选中代理方法,你也就没有机会还原cell的默认样式。
4. UICollectionView 4.1 通过屏幕点击改变的选中状态回调给代理 //选中-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath; //非选中-(void)collectionView:(UICollectionView*)collectionView didDeselectItemAtIndexPath:(NSIndexPath*)indexPath;
4.2 代码设置默认选中状态 (要等数据加载完成之后再调用) 执行方法的主体:UICollectionView对象
//选中-(void)selectItemAtIndexPath:(nullable NSIndexPath*)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition; //非选中-(void)deselectItemAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated;
注意的是:
类似的,上述代码强制设置某单元格选中或者不选中那一刻,都不会回调选中代理方法,也不会发出通知。
之后,通过屏幕点击选中其它cell的时候,可以执行- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath; 代理方法,你就有机会还原cell的默认样式。
4.3 补充:代码设置选中状态 执行方法的主体:cell对象
-(void)setSelected:(BOOL)selected;
注意的是:
类似的,这种方法改变cell的选中状态时,当屏幕选中其它cell的时候,UITableView并不会执行- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath ; 代理方法。
5. 比较 比如,下面两种方案
NSIndexPath*indexPath=[NSIndexPath indexPathForRow:0inSection:0]; UICollectionViewCell*cell=[self.collectionView cellForItemAtIndexPath:indexPath]; [cell setSelected:YES];
上述方案仅仅改变cell的属性,但当屏幕点击选中其它cell的时候,也不会执行原cell的非选中代理。
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0inSection:0]animated:YES scrollPosition:UICollectionViewScrollPositionNone];
上述方案改变了cell的属性,而且当选中其它cell的时候,会执行非选中代理。
6. 手动执行代理 上述两张方案的区别在于,设置选中状态完后,屏幕点击其它cell时,一个执行原cell的didDeselect方法,一个不执行。
相同点在于,手动设置选中的时候,都是不会执行didSelect方法的。
如果你真的想在改变选中状态的时候执行didSelect代理,那么可以手动执行:
【UITableView&UICollectionView设置单元格的默认选中状态】NSIndexPath*indexPath=[NSIndexPath indexPathForRow:0inSection:0]; [mytableview selectRowAtIndexPath:[NSIndexPath indexPathForRow:0inSection:0]animated:YES scrollPosition:UITableViewScrollPositionTop]; if([mytableview.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]){[mytableview.delegate tableView:mytableview didSelectRowAtIndexPath:indexPath]; }

    推荐阅读