本文实例为大家分享了iOS实现折叠单元格的具体代码,供大家参考,具体内容如下
<>强思路强>
点击按钮或细胞单元格来进行展开收缩,同时使用一个BOOL值记录单元格展开收缩状态,根据BOOL值对视图的高度和按钮的图像进行实时变更。
在执行- - - - - - (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath(点击当前单元格)方法时,收缩单元格,显示当前点击的单元格的内容。这一步骤的实现是<强>对存储单元格内容的可变数组进行更改。
强>
<强>代码强>
//ViewController。h中 # import & lt; UIKit/UIKit.h> @ interface ViewController: ui @ property UITableView *视图; @ property UIButton *按钮; @ property NSMutableArray * imageViewArr; @ property NSMutableArray * labelArr; @ property BOOL选择;//记录单元格展开收缩状态 @end
//ViewController。米中 #进口“ViewController.h” #进口“ViewTableViewCell.h” #进口“Masonry.h” @ interface ViewController () & lt; UITableViewDelegate UITableViewDataSource> @end @ implementation ViewController - (void) viewDidLoad { (超级viewDidLoad); self。view。写成backgroundColor=[用户界面颜色colorWithWhite: 0.92 alpha: 1]; _imageViewArr=[[NSMutableArray alloc] initWithObjects: @“1”@“2”@“3”@“4”@“5”, nil); _labelArr=[[NSMutableArray alloc] initWithObjects: @”发起群聊”,@“添加朋友”,@“扫一扫”,@“收付款”,@“帮助与反馈”,nil); _tableView=[[UITableView alloc] init); (自我。视图addSubview _tableView):; _tableView.frame=CGRectMake (100, 100, 130, 35);//以下使用砖石对tableView进行约束,约束不是很规范可忽略//[_tableView mas_makeConstraints: ^ (MASConstraintMaker *使){//make.height.mas_offset (self.view.frame.size.height * 0.0485);//make.width.mas_offset (self.view.frame.size.width * 0.335);//make.left.equalTo (self.view.mas_left) .offset (self.view.frame.size.width * 0.6);//make.top.equalTo (self.view.mas_top) .offset (self.view.frame.size.height * 0.046);////}); _tableView.delegate=自我; _tableView。数据源=自我; [_tableView registerClass (ViewTableViewCell类):forCellReuseIdentifier: @“细胞”); _button=[UIButton buttonWithType UIButtonTypeCustom):; (自我。视图addSubview _button):; [_button mas_makeConstraints: ^ (MASConstraintMaker *使){ make.left.equalTo (_tableView.mas_right) .offset (-28); make.top.equalTo (_tableView.mas_top) .offset (4); make.height.mas_offset (self.view.frame.size.height * 0.0495 * 0.68); make.width.mas_offset (self.view.frame.size.width * 0.335 * 0.22); }); [_button setImage:(界面图像imageNamed: @“瘦”forState: UIControlStateNormal); [_button addTarget:自我行动:@ selector(媒体)forControlEvents: UIControlEventTouchUpInside);//默认单元格为收缩选择为0 _select=0; } ——(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView { 返回1; } ——(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection:也(NSInteger)节{//根据选择的值来判断收缩展开状态,返回相应的行数 如果(_select==0) { 返回1; 其他}{ 返回5; } } ——(CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath { 返回40; } ——(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ViewTableViewCell *细胞=[tableView dequeueReusableCellWithIdentifier: @“细胞”forIndexPath: indexPath); cell.iimageView。形象=[界面图像imageNamed: _imageViewArr [indexpath。row]]; cell.label。文本=[NSString stringWithString: _labelArr [indexpath。row]]; 返回单元; }//点击当前单元格 - (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {//记录当前单元格的imageView和标签的内容 NSString * imageViewStr=[NSString stringWithString: _imageViewArr [indexpath。row]]; NSString * labelStr=[NSString stringWithString: _labelArr [indexpath。row]];//将当前单元格的内容插入可变数组,作为第一个元素 [_imageViewArr insertObject: imageViewStr atIndex: 0]; [_labelArr insertObject: labelStr atIndex: 0];//同时删除可变数组中当前单元格的原本所在位置 [_imageViewArr removeObjectAtIndex: indexPath。行+ 1]; [_labelArr removeObjectAtIndex: indexPath。行+ 1];//更新视图 [_tableView reloadData];//调用媒体方法,变更tableView的高度和按钮的图像 (自我出版社); } - (void)按{//通过判断选择的值,判断单元格的展开与收缩,更改tableView的高度和按钮的图像 如果(_select==0) { _select=1; _tableView.frame=CGRectMake (100, 100, 130, 200);//以下使用砖石对tableView进行更新约束(以下代码为更新视图的高度)//[_tableView mas_updateConstraints: ^ (MASConstraintMaker *使){//make.height.mas_offset (200);//}); [_button setImage:(界面图像imageNamed: @“凯”]forState: UIControlStateNormal); 其他}{ _select=0; _tableView.frame=CGRectMake (100, 100, 130, 35);//[_tableView mas_updateConstraints: ^ (MASConstraintMaker *使){//make.height.mas_offset (self.view.frame.size.height * 0.0485);//}); [_button setImage:(界面图像imageNamed: @“瘦”forState: UIControlStateNormal); } [_tableView reloadData]; } @endiOS实现折叠单元格