iOS开发之tableView cell的展开收回功能实现代码
一、实现方法
例如好友分组,分为好友和陌生人两组,实现点击好友和陌生人展开或收回该分组对应的cell的功能。
实现:可以分组对应tableView的section,点击section展开和收回cell。
创建一个临时数组selectedArr存储需要展开的section。点击section是判断selectedArr是否包含该组,如果包含则移除,不包含则添加到selectedArr。
展示selectedArr包含组的cell。
二、代码实现
#import"ZCellGroupController.h" @interfaceZCellGroupController()@property(nonatomic,strong)UITableView*tableView; @property(nonatomic,strong)NSArray*titleArray;//分组 @property(nonatomic,strong)NSArray*friendsArray;//每组对应内容 @property(nonatomic,strong)NSMutableArray*selectedArr;//存储需要展开的cell组 @end @implementationZCellGroupController -(void)viewDidLoad{ [superviewDidLoad]; self.selectedArr=[[NSMutableArrayalloc]init]; [selfaddTableView]; [selfaddData]; } -(void)addTableView{ UITableView*tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,kSCREENWIDTH,kScreenHeight)style:UITableViewStyleGrouped]; self.tableView=tableView; tableView.delegate=self; tableView.dataSource=self; tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine; [self.viewaddSubview:_tableView]; } -(void)addData{ self.titleArray=[NSArrayarrayWithObjects:@"好友",@"陌生人",nil]; self.friendsArray=[NSArrayarrayWithObjects: @[@"A",@"B",@"C",@"D",@"E",@"F"], @[@"陌生1",@"陌生2",@"陌生3",@"陌生4",@"陌生5",@"陌生6",@"陌生7",@"陌生8",@"陌生9",@"陌生10",@"陌生11",@"陌生12",@"陌生13",@"陌生14"],nil]; } #pragmamark--tableViewDelegate -(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{ returnself.titleArray.count; } -(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{ NSString*sectionStr=[NSStringstringWithFormat:@"%ld",(long)section]; NSIntegernum; //如果selectedArr不包含section,该分组返回number为0; if([self.selectedArrcontainsObject:sectionStr]){ NSArray*arrayData=self.friendsArray[section]; num=arrayData.count; }else{ num=0; } returnnum; } -(CGFloat)tableView:(UITableView*)tableViewheightForHeaderInSection:(NSInteger)section{ return40; } -(UIView*)tableView:(UITableView*)tableViewviewForHeaderInSection:(NSInteger)section{ UIView*view=[[UIViewalloc]initWithFrame:CGRectMake(0,0,kSCREENWIDTH,40)]; view.backgroundColor=[UIColorwhiteColor]; UILabel*titleLabel=[[UILabelalloc]initWithFrame:CGRectMake(10,5,kSCREENWIDTH-20,30)]; titleLabel.text=self.titleArray[section]; [viewaddSubview:titleLabel]; //添加点击事件 UIButton*btn=[[UIButtonalloc]initWithFrame:CGRectMake(0,0,kSCREENWIDTH,40)]; btn.tag=100+section; [btnaddTarget:selfaction:@selector(viewBtnClick:)forControlEvents:UIControlEventTouchUpInside]; [viewaddSubview:btn]; returnview; } -(void)viewBtnClick:(UIButton*)btn{ NSString*string=[NSStringstringWithFormat:@"%ld",btn.tag-100]; if([self.selectedArrcontainsObject:string]){ [self.selectedArrremoveObject:string]; }else{ [self.selectedArraddObject:string]; } NSLog(@"selectedArr:%@",self.selectedArr); [_tableViewreloadData]; } -(CGFloat)tableView:(UITableView*)tableViewheightForFooterInSection:(NSInteger)section{ return10; } -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{ NSString*sectionStr=[NSStringstringWithFormat:@"%ld",(long)indexPath.section]; staticNSString*cellID=@"testCellID"; UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:cellID]; if(!cell){ cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellID]; cell.selectionStyle=UITableViewCellSelectionStyleGray; } NSArray*arrayData=self.friendsArray[indexPath.section]; if([self.selectedArrcontainsObject:sectionStr]){ cell.textLabel.text=arrayData[indexPath.row]; } returncell; } -(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{ return45; } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。