欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS開發(fā)之tableView cell的展開收回功能實現(xiàn)代碼

 更新時間:2018年01月26日 16:51:36   作者:勤勞的小QQ  
本文介紹了iOS開發(fā)之tableView cell的展開收回功能實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、實現(xiàn)方法

例如好友分組,分為好友和陌生人兩組,實現(xiàn)點擊好友和陌生人展開或收回該分組對應(yīng)的cell的功能。

實現(xiàn):可以分組對應(yīng)tableView的section,點擊section展開和收回cell。

創(chuàng)建一個臨時數(shù)組selectedArr存儲需要展開的section。點擊section是判斷selectedArr是否包含該組,如果包含則移除,不包含則添加到selectedArr。

展示selectedArr包含組的cell。

二、代碼實現(xiàn)

#import "ZCellGroupController.h"

@interface ZCellGroupController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *titleArray;//分組
@property (nonatomic, strong) NSArray *friendsArray;//每組對應(yīng)內(nèi)容
@property (nonatomic, strong) NSMutableArray *selectedArr;//存儲需要展開的cell組

@end

@implementation ZCellGroupController


-(void)viewDidLoad {
  [super viewDidLoad];
  self.selectedArr = [[NSMutableArray alloc]init];
  [self addTableView];
  [self addData];
}
- (void)addTableView{
  UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, kScreenHeight) style:UITableViewStyleGrouped];
  self.tableView = tableView;
  tableView.delegate = self;
  tableView.dataSource = self;
  tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  [self.view addSubview:_tableView];
}
- (void)addData{
  self.titleArray = [NSArray arrayWithObjects:@"好友",@"陌生人", nil];
  self.friendsArray = [NSArray arrayWithObjects:
                       @[@"A",@"B",@"C",@"D",@"E",@"F"],
                       @[@"陌生1",@"陌生2",@"陌生3",@"陌生4",@"陌生5",@"陌生6",@"陌生7",@"陌生8",@"陌生9",@"陌生10",@"陌生11",@"陌生12",@"陌生13",@"陌生14"],nil];
}

#pragma mark --tableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  return self.titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  NSString *sectionStr = [NSString stringWithFormat:@"%ld",(long)section];
  NSInteger num ;
  //如果selectedArr不包含section,該分組返回number為0;
  if ([self.selectedArr containsObject:sectionStr]) {
    NSArray *arrayData = self.friendsArray[section];
    
    num = arrayData.count ;
  }else{
    num = 0;
  }
  return num;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 40)];
  view.backgroundColor = [UIColor whiteColor];
  UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5,kSCREENWIDTH-20 , 30)];
  titleLabel.text = self.titleArray[section];
  [view addSubview:titleLabel];
  
  //添加點擊事件
  UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kSCREENWIDTH, 40)];
  btn.tag = 100+section;
  [btn addTarget:self action:@selector(viewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  [view addSubview:btn];
  return view;
}
- (void)viewBtnClick:(UIButton *)btn{
  NSString *string = [NSString stringWithFormat:@"%ld",btn.tag - 100];
  if ([self.selectedArr containsObject:string]) {
    [self.selectedArr removeObject:string];
  }else{
    [self.selectedArr addObject:string];
  }
  NSLog(@"selectedArr:%@",self.selectedArr);

  [_tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
  NSString *sectionStr = [NSString stringWithFormat:@"%ld",(long)indexPath.section];
  static NSString *cellID = @"testCellID";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

  }
  
  NSArray *arrayData = self.friendsArray[indexPath.section];
  
  if ([self.selectedArr containsObject:sectionStr]) {
    cell.textLabel.text = arrayData[indexPath.row];
  }
  return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 45;
}
@end

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

相關(guān)文章

  • iOS關(guān)鍵字static extern const使用示例詳解

    iOS關(guān)鍵字static extern const使用示例詳解

    這篇文章主要為大家介紹了iOS關(guān)鍵字static extern const使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • 詳解iOS中position:fixed吸底時的滑動出現(xiàn)抖動的解決方案

    詳解iOS中position:fixed吸底時的滑動出現(xiàn)抖動的解決方案

    這篇文章主要介紹了詳解iOS中position:fixed吸底時的滑動出現(xiàn)抖動的解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 設(shè)計模式中的Memento備忘錄模式的在iOS App開發(fā)中的運用

    設(shè)計模式中的Memento備忘錄模式的在iOS App開發(fā)中的運用

    這篇文章主要介紹了設(shè)計模式中的Memento備忘錄模式的在iOS App開發(fā)中的運用,Memento著重于捕獲和具體化當(dāng)前對象的內(nèi)部狀態(tài),需要的朋友可以參考下
    2016-03-03
  • iOS應(yīng)用中UICollectionViewCell定制Button

    iOS應(yīng)用中UICollectionViewCell定制Button

    這篇文章主要介紹了iOS應(yīng)用中UICollectionViewCell如何定制Button,設(shè)置每行顯示的按鈕的個數(shù),自定制按鈕的顯示樣式,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 去除IOS蘋果手機自帶按鈕樣式的方法(推薦)

    去除IOS蘋果手機自帶按鈕樣式的方法(推薦)

    下面小編就為大家分享一篇去除IOS蘋果手機自帶按鈕樣式的方法(推薦),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS開發(fā)中指紋識別簡單介紹

    iOS開發(fā)中指紋識別簡單介紹

    指紋識別是在iOS8.0以后才推出的,所以我們?nèi)绻氚阎讣y集成到我們的APP當(dāng)中,我們首先就要在代碼中判斷iOS版本。接下來通過本文給大家分享iOS開發(fā)中指紋識別簡單介紹,需要的朋友參考下吧
    2017-11-11
  • ios學(xué)習(xí)筆記之基礎(chǔ)數(shù)據(jù)類型的轉(zhuǎn)換

    ios學(xué)習(xí)筆記之基礎(chǔ)數(shù)據(jù)類型的轉(zhuǎn)換

    在編碼過程中,數(shù)據(jù)的處理是必要的。眾多數(shù)據(jù)中,NSString、NSData、NSArray、 NSDictionary等數(shù)據(jù)類型是常用的,對付它們?nèi)菀?,但是在多個數(shù)據(jù)類型之間轉(zhuǎn)換就需要技巧了。本文主要給大家介紹ios中基礎(chǔ)數(shù)據(jù)類型的轉(zhuǎn)換,有需要的下面來一起看看吧。
    2016-11-11
  • IOS開發(fā)中使用writeToFile時的注意事項

    IOS開發(fā)中使用writeToFile時的注意事項

    本篇文章主要介紹了開發(fā)中使用writeToFile時的注意事項,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • iOS中PNChart與UITableView的聯(lián)動示例詳解

    iOS中PNChart與UITableView的聯(lián)動示例詳解

    PNChart是個界面很漂亮的圖表第三方庫,UITableView則不用過多介紹了,各位iOS開發(fā)者們都知道,下面這篇文章主要給大家介紹了關(guān)于iOS中PNChart與UITableView的聯(lián)動的相關(guān)資料,需要的朋友可以參考下
    2018-07-07
  • iOS開發(fā)UICollectionView實現(xiàn)拖拽效果

    iOS開發(fā)UICollectionView實現(xiàn)拖拽效果

    這篇文章主要為大家詳細介紹了iOS開發(fā)UICollectionView實現(xiàn)拖拽效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01

最新評論