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

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

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

2020-07-26 02:30:59
字體:
供稿:網(wǎng)友

一、實(shí)現(xiàn)方法

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

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

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

展示selectedArr包含組的cell。

二、代碼實(shí)現(xiàn)

#import "ZCellGroupController.h"@interface ZCellGroupController ()<UITableViewDelegate, UITableViewDataSource>@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) NSArray *titleArray;//分組@property (nonatomic, strong) NSArray *friendsArray;//每組對(duì)應(yīng)內(nèi)容@property (nonatomic, strong) NSMutableArray *selectedArr;//存儲(chǔ)需要展開的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];    //添加點(diǎn)擊事件  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

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 洪雅县| 翁牛特旗| 奉贤区| 金昌市| 固阳县| 汤阴县| 新化县| 招远市| 赞皇县| 奇台县| 繁峙县| 马尔康县| 荣成市| 环江| 温州市| 旬邑县| 延吉市| 宁河县| 十堰市| 察隅县| 广灵县| 施甸县| 家居| 泾源县| 班玛县| 黎平县| 内丘县| 宾川县| 镇坪县| 清涧县| 腾冲县| 临汾市| 金秀| 汨罗市| 巴东县| 铜梁县| 安平县| 临安市| 喀喇沁旗| 河东区| 广南县|