1. 場(chǎng)景需求
一個(gè)表格視圖(或者宮格視圖)中,當(dāng)一個(gè)單元格被選中時(shí)設(shè)置彩色樣式,選中其它單元格時(shí)設(shè)置灰色樣式。
2. 一個(gè)思路
通過實(shí)現(xiàn)選中和非選擇的代理,以在適當(dāng)?shù)臅r(shí)機(jī)進(jìn)行UI更新操作。
3. UITableView
3.1 通過屏幕點(diǎn)擊改變的選中狀態(tài)回調(diào)給代理
//選中-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;//非選中-(void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath;
3.2 代碼設(shè)置默認(rèn)選中狀態(tài) (要等數(shù)據(jù)加載完成之后再調(diào)用)
執(zhí)行方法的主體:tableview對(duì)象
//選中-(void)selectRowAtIndexPath:(nullable NSIndexPath*)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;//非選中-(void)deselectRowAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated;
注意的是:
上述代碼強(qiáng)制設(shè)置某單元格選中或者不選中那一刻,都不會(huì)回調(diào)tableview的選中代理方法,也不會(huì)發(fā)出通知UITableViewSelectionDidChangeNotification。
之后,通過屏幕點(diǎn)擊選中其它c(diǎn)ell的時(shí)候,可以執(zhí)行- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;代理方法,你就有機(jī)會(huì)還原cell的默認(rèn)樣式。
3.3 補(bǔ)充:代碼設(shè)置默認(rèn)選中狀態(tài)
執(zhí)行方法的主體:cell對(duì)象
-(void)setSelected:(BOOL)selected animated:(BOOL)animated;// animate between regular and selected state
注意的是:
這種方法改變cell的選中狀態(tài)時(shí),當(dāng)通過屏幕點(diǎn)擊選中其它c(diǎn)ell的時(shí)候,UITableView并不會(huì)執(zhí)行- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;的非選中代理方法,你也就沒有機(jī)會(huì)還原cell的默認(rèn)樣式。
4. UICollectionView
4.1 通過屏幕點(diǎn)擊改變的選中狀態(tài)回調(diào)給代理
//選中-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath;//非選中-(void)collectionView:(UICollectionView*)collectionView didDeselectItemAtIndexPath:(NSIndexPath*)indexPath;
4.2 代碼設(shè)置默認(rèn)選中狀態(tài) (要等數(shù)據(jù)加載完成之后再調(diào)用)
執(zhí)行方法的主體:UICollectionView對(duì)象
//選中-(void)selectItemAtIndexPath:(nullable NSIndexPath*)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition;//非選中-(void)deselectItemAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated;
注意的是:
類似的,上述代碼強(qiáng)制設(shè)置某單元格選中或者不選中那一刻,都不會(huì)回調(diào)選中代理方法,也不會(huì)發(fā)出通知。
之后,通過屏幕點(diǎn)擊選中其它c(diǎn)ell的時(shí)候,可以執(zhí)行- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;代理方法,你就有機(jī)會(huì)還原cell的默認(rèn)樣式。
4.3 補(bǔ)充:代碼設(shè)置選中狀態(tài)
執(zhí)行方法的主體:cell對(duì)象
-(void)setSelected:(BOOL)selected;
注意的是:
類似的,這種方法改變cell的選中狀態(tài)時(shí),當(dāng)屏幕選中其它c(diǎn)ell的時(shí)候,UITableView并不會(huì)執(zhí)行- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath ;代理方法。
5. 比較
比如,下面兩種方案
NSIndexPath*indexPath=[NSIndexPath indexPathForRow:0inSection:0];UICollectionViewCell*cell=[self.collectionView cellForItemAtIndexPath:indexPath];[cell setSelected:YES];
上述方案僅僅改變cell的屬性,但當(dāng)屏幕點(diǎn)擊選中其它c(diǎn)ell的時(shí)候,也不會(huì)執(zhí)行原cell的非選中代理。
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0inSection:0]animated:YES scrollPosition:UICollectionViewScrollPositionNone];
上述方案改變了cell的屬性,而且當(dāng)選中其它c(diǎn)ell的時(shí)候,會(huì)執(zhí)行非選中代理。
6. 手動(dòng)執(zhí)行代理
上述兩張方案的區(qū)別在于,設(shè)置選中狀態(tài)完后,屏幕點(diǎn)擊其它c(diǎn)ell時(shí),一個(gè)執(zhí)行原cell的didDeselect方法,一個(gè)不執(zhí)行。
相同點(diǎn)在于,手動(dòng)設(shè)置選中的時(shí)候,都是不會(huì)執(zhí)行didSelect方法的。
如果你真的想在改變選中狀態(tài)的時(shí)候執(zhí)行didSelect代理,那么可以手動(dòng)執(zhí)行:
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];}