iOS tableView實(shí)現(xiàn)單選和多選的實(shí)例代碼
今天在項(xiàng)目中遇到了tableView的單選需求,現(xiàn)在總結(jié)一下,用一個(gè)簡(jiǎn)單的demo實(shí)現(xiàn)了簡(jiǎn)單的單選和多選兩個(gè)功能.先看下效果圖:
1:首先實(shí)現(xiàn)下單選
1:使用一個(gè)變量記錄選中的行
@property (assign, nonatomic) NSIndexPath *selIndex; //單選選中的行
2:設(shè)置tableView數(shù)據(jù),共2組,每組10行,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; }
3:實(shí)現(xiàn)tableView的點(diǎn)擊方法,每次點(diǎn)擊記錄點(diǎn)擊的索引,取消之前的選擇行,將當(dāng)前選擇的行打鉤
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //取消之前的選擇 UITableViewCell *celled = [tableView cellForRowAtIndexPath:_selIndex]; celled.accessoryType = UITableViewCellAccessoryNone; //記錄當(dāng)前的選擇的位置 _selIndex = indexPath; //當(dāng)前選擇的打鉤 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; }
4:列表滾動(dòng)時(shí),判斷是否為選中的行,如果是cell是選中的那一行,就設(shè)置cell的accessoryType為UITableViewCellAccessoryCheckmark,到這里單選就實(shí)現(xiàn)完成了
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellid = @"cellid"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid]; } cell.textLabel.text = [NSString stringWithFormat:@"第%zi組,第%zi行",indexPath.section+1,indexPath.row]; if (_selIndex == indexPath) { cell.accessoryType = UITableViewCellAccessoryCheckmark; }else{ cell.accessoryType = UITableViewCellAccessoryNone; } return cell; }
2:下面實(shí)現(xiàn)多選
1:使用一個(gè)數(shù)組記錄選中的行
@property (strong, nonatomic) NSMutableArray *selectIndexs; //多選選中的行
2:使用一個(gè)變量判斷是單選還是多選狀態(tài)
@property (nonatomic, assign) BOOL isSingle; //單選還是多選
3:導(dǎo)航欄右側(cè)按鈕設(shè)置為單選和雙選的切換按鈕,并初始化多選記錄數(shù)組
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"多選" style:UIBarButtonItemStylePlain target:self action:@selector(singleSelect)]; self.navigationItem.rightBarButtonItem = rightItem; //初始化多選數(shù)組 _selectIndexs = [NSMutableArray new];
4:點(diǎn)擊導(dǎo)航欄上的切換按鈕切換單選還是多選狀態(tài)
//單選還是多選按鈕點(diǎn)擊事件 -(void)singleSelect{ _isSingle = !_isSingle; if (_isSingle) { self.navigationItem.rightBarButtonItem.title = @"多選"; self.title = @"(單選)"; //切換為單選的時(shí)候,清除多選數(shù)組,重新加載列表 [self.selectIndexs removeAllObjects]; [self.tableView reloadData]; }else{ self.title = @"(多選)"; self.navigationItem.rightBarButtonItem.title = @"單選"; } }
5:為tableView的點(diǎn)擊方法中加上單選還是多選的狀態(tài)判斷,多選的話,將點(diǎn)擊的行加入到多選索引數(shù)組中去,然后改變?cè)撔械腸ell.accessoryType,重復(fù)點(diǎn)擊就做反操作
//選中某一行 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (_isSingle) { //單選 //取消之前的選擇 UITableViewCell *celled = [tableView cellForRowAtIndexPath:_selIndex]; celled.accessoryType = UITableViewCellAccessoryNone; //記錄當(dāng)前的選擇的位置 _selIndex = indexPath; //當(dāng)前選擇的打鉤 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; }else{ //多選 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { //如果為選中狀態(tài) cell.accessoryType = UITableViewCellAccessoryNone; //切換為未選中 [_selectIndexs removeObject:indexPath]; //數(shù)據(jù)移除 }else { //未選中 cell.accessoryType = UITableViewCellAccessoryCheckmark; //切換為選中 [_selectIndexs addObject:indexPath]; //添加索引數(shù)據(jù)到數(shù)組 } } }
6:在cellForRow代理方法中同樣加入單選多選的判斷,在滾動(dòng)列表是加載列表,判斷是否選中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellid = @"cellid"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid]; } cell.textLabel.text = [NSString stringWithFormat:@"第%zi組,第%zi行",indexPath.section+1,indexPath.row]; if (_isSingle) { //單選 if (_selIndex == indexPath) { cell.accessoryType = UITableViewCellAccessoryCheckmark; }else{ cell.accessoryType = UITableViewCellAccessoryNone; } return cell; }else{ //多選 cell.accessoryType = UIAccessibilityTraitNone; for (NSIndexPath *index in _selectIndexs) { if (indexPath == index) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } } } return cell; }
到這里就完成了,沒什么技術(shù)含量,有需求的可以參考下,有好的想法可以多多交流,項(xiàng)目在github的地址.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS App設(shè)計(jì)模式開發(fā)之適配器模式使用的實(shí)戰(zhàn)演練
這篇文章主要介紹了iOS App設(shè)計(jì)模式開發(fā)之適配器模式的使用實(shí)例,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-03-03iOS自定義UICollectionViewLayout實(shí)現(xiàn)瀑布流布局
這篇文章主要為大家詳細(xì)介紹了iOS自定義UICollectionViewLayout實(shí)現(xiàn)瀑布流布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12iOS實(shí)現(xiàn)只有底部邊框線的輸入框示例代碼
這篇文章給大家分享了一種利用iOS實(shí)現(xiàn)只有底部邊框線的輸入框,其實(shí)這個(gè)效果也挺常見的,本文給出了示例代碼,下面來看看如何實(shí)現(xiàn)這種效果。2016-09-09iOS 禁止按鈕在一定時(shí)間內(nèi)連續(xù)點(diǎn)擊
本文主要介紹了iOS中禁止按鈕在一定時(shí)間內(nèi)連續(xù)點(diǎn)擊的方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題
今天小編就為大家分享一篇解決ios h5 input輸入框被輸入法彈出一塊區(qū)域的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08ios8 UITableView設(shè)置 setSeparatorInset UIEdgeInsetsZero不起作用的解決
這篇文章主要介紹了ios8 UITableView設(shè)置 setSeparatorInset UIEdgeInsetsZero不起作用的解決辦法(去掉15px空白間距)的相關(guān)資料,需要的朋友可以參考下2016-02-02iOS如何實(shí)現(xiàn)強(qiáng)制轉(zhuǎn)屏、強(qiáng)制橫屏和強(qiáng)制豎屏的實(shí)例代碼
本篇文章主要介紹了iOS如何實(shí)現(xiàn)強(qiáng)制轉(zhuǎn)屏、強(qiáng)制橫屏和強(qiáng)制豎屏的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07