Folding cells in IOS

Time:2021-6-9

In this paper, we share the specific code of folding cells in IOS for your reference. The specific content is as follows

thinking

Click the button or cell to expand and contract, and use a bool value to record the expansion and contraction status of the cell. According to the bool value, the height of tableview and the image of button are changed in real time.

Note:

When the – (void) tableview: (uitableview *) tableview didselectrowatindexpath: (NSIndexPath *) indexpath method is executed, the cell is shrunk to display the contents of the currently clicked cell. What is the realization of this stepMake changes to the variable array that stores the contents of the cell.

code

//In viewcontroller. H

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property UITableView *tableView;
@property UIButton *button;   
@property NSMutableArray *imageViewArr; 
@property NSMutableArray *labelArr;  
@property BOOL select;    // Record cell expansion and contraction status

@end
//In viewcontroller. M

#import "ViewController.h"
#import "ViewTableViewCell.h"
#import "Masonry.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];
 
 self.view.backgroundColor = [UIColor colorWithWhite:0.92 alpha:1];
 
 _imageViewArr = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
 _ Labelarr = [[nsmutablearray alloc] initwithobjects: @ "initiate group chat", @ "add friends", @ "scan", @ "collection and payment", @ "help and feedback", nil];
 
 _tableView = [[UITableView alloc] init];
 [self.view addSubview:_tableView];

 _tableView.frame = CGRectMake(100, 100, 130, 35);
 //Below, we use massionry to constrain the tableview, which is not very standard and can be ignored
// [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//  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 = self;
 _tableView.dataSource = self;
 [_tableView registerClass:[ViewTableViewCell class] forCellReuseIdentifier:@"cell"];
 
 _button = [UIButton buttonWithType:UIButtonTypeCustom];
 [self.view addSubview:_button];

 [_button mas_makeConstraints:^(MASConstraintMaker *make) {
  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:[UIImage imageNamed:@"shou"] forState:UIControlStateNormal];
 [_button addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
 //The default cell is shrink and select is 0
 _select = 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 //According to the value of select, the state of contraction and expansion is judged, and the corresponding number of rows is returned 
 if(_select == 0) {
  return 1;
 } else {
  return 5;
 }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
 return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
 ViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
 cell.iimageView.image = [UIImage imageNamed:_imageViewArr[indexPath.row]];
 cell.label.text = [NSString stringWithString:_labelArr[indexPath.row]];
 return cell;
}

//Click the current cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 //Record the contents of ImageView and label of the current cell
 NSString *imageViewStr = [NSString stringWithString:_imageViewArr[indexPath.row]];
 NSString *labelStr = [NSString stringWithString:_labelArr[indexPath.row]];

 //Inserts the contents of the current cell into a variable array as the first element
 [_imageViewArr insertObject:imageViewStr atIndex:0];
 [_labelArr insertObject:labelStr atIndex:0];
 
 //At the same time, delete the original position of the current cell in the variable array
 [_imageViewArr removeObjectAtIndex:indexPath.row + 1];
 [_labelArr removeObjectAtIndex:indexPath.row + 1];
 
 //Update tableview
 [_tableView reloadData];
 
 //Call the press method to change the height of tableview and the image of button
 [self press];
 
}


- (void)press {

 //By judging the value of select, judge the expansion and contraction of cells, change the height of tableview and the image of button
 if (_select == 0) {
  _select = 1;
  
  _tableView.frame = CGRectMake(100, 100, 130, 200);
  
  //The following uses masonry to update the tableview constraint (the following code is the height of the updated tableview)
//  [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
//   make.height.mas_offset(200);
//  }];

  [_button setImage:[UIImage imageNamed:@"kai"] forState:UIControlStateNormal];
  
 } else {
  _select = 0;
  _tableView.frame = CGRectMake(100, 100, 130, 35);
//  [_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
//   make.height.mas_offset(self.view.frame.size.height * 0.0485);
//  }];

  [_button setImage:[UIImage imageNamed:@"shou"] forState:UIControlStateNormal];
 }
 [_tableView reloadData];
}

@end
//In viewtableviewcell. H

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ViewTableViewCell : UITableViewCell

@property UIImageView *iimageView;
@property UILabel *label;

@end
//In viewtableviewcell. M

#import "ViewTableViewCell.h"

@implementation ViewTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
 
 _iimageView = [[UIImageView alloc] init];
 [self.contentView addSubview:_iimageView];
 
 _label = [[UILabel alloc] init];
 [self.contentView addSubview:_label];
 return self;
}

- (void)layoutSubviews {
 [super layoutSubviews];
 _iimageView.frame = CGRectMake(5, 5, 25, 25);
 _label.frame = CGRectMake(37, 5, 80, 25);
 _label.font = [UIFont systemFontOfSize:15];
}

@end

The effect picture is as follows

Initial state

Click cell or click button to display as follows:

Click any cell, for example, click Scan to retrieve the cell, as shown in the figure

Expand the cell again, and the cell contents are as follows:

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

“Quick start typescript” typescript basic syntax

1、 Typescript Introduction: Typescript is a programming language (TS) developed by Microsoft and a superset of JavaScript. Typescript extends the syntax of JavaScript, adds some new syntax following ES6 specification, and has the characteristics of class based object-oriented programming. Any existing JavaScript program can run in typescript running environment without any changes. Advantages of typescript […]