国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS tableView實現(xiàn)單選和多選的實例代碼

2019-10-21 18:45:21
字體:
供稿:網(wǎng)友

今天在項目中遇到了tableView的單選需求,現(xiàn)在總結(jié)一下,用一個簡單的demo實現(xiàn)了簡單的單選和多選兩個功能.先看下效果圖:

ios,tableview,單選和多選,單選,多選

1:首先實現(xiàn)下單選

1:使用一個變量記錄選中的行

@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:實現(xiàn)tableView的點擊方法,每次點擊記錄點擊的索引,取消之前的選擇行,將當(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:列表滾動時,判斷是否為選中的行,如果是cell是選中的那一行,就設(shè)置cell的accessoryType為UITableViewCellAccessoryCheckmark,到這里單選就實現(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:下面實現(xiàn)多選

1:使用一個數(shù)組記錄選中的行

@property (strong, nonatomic) NSMutableArray  *selectIndexs; //多選選中的行

2:使用一個變量判斷是單選還是多選狀態(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:點擊導(dǎo)航欄上的切換按鈕切換單選還是多選狀態(tài)

//單選還是多選按鈕點擊事件-(void)singleSelect{  _isSingle = !_isSingle;  if (_isSingle) {    self.navigationItem.rightBarButtonItem.title = @"多選";    self.title = @"(單選)";    //切換為單選的時候,清除多選數(shù)組,重新加載列表    [self.selectIndexs removeAllObjects];    [self.tableView reloadData];  }else{    self.title = @"(多選)";    self.navigationItem.rightBarButtonItem.title = @"單選";  }}

5:為tableView的點擊方法中加上單選還是多選的狀態(tài)判斷,多選的話,將點擊的行加入到多選索引數(shù)組中去,然后改變該行的cell.accessoryType,重復(fù)點擊就做反操作

//選中某一行-(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代理方法中同樣加入單選多選的判斷,在滾動列表是加載列表,判斷是否選中

- (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ù)含量,有需求的可以參考下,有好的想法可以多多交流,項目在github的地址.

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 五华县| 辰溪县| 南昌县| 大冶市| 尼玛县| 沐川县| 常德市| 沁源县| 杭州市| 莲花县| 晴隆县| 镇巴县| 中西区| 互助| 民丰县| 延津县| 政和县| 永吉县| 志丹县| 江山市| 岑溪市| 临湘市| 泰来县| 凤翔县| 阿拉善盟| 天津市| 瑞安市| 靖州| 张家口市| 明溪县| 嘉荫县| 阜宁县| 遂平县| 班戈县| 成武县| 邵阳县| 囊谦县| 晋城| 惠来县| 呈贡县| 北票市|