Write a lot of tableview, just the common tableview application scenario to introduce to you, (^__^) hee hee…… A simple tableView that can expand and shrink, similar to the QQ friends class table.

First, hesitation is a simple demo, where we construct our own data.

for (int i = 0; i < 4; i++) {
    BaseDataModel *model = [[BaseDataModel alloc] init];
    model.isOpen = NO;
    NSString *name = [NSString stringWithFormat:@"Section:%d",i];
    model.name = name;
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
    for (int j = 0; j < 4; j++) {
        NSString *cellName = [NSString stringWithFormat:@"Cell:%d",j];
        [array addObject:cellName];
    }
    model.dataArray = array;
    [self.dataArray addObject:model];
}
Copy the code

BaseModel is one of our Model classes. OK, so once we’ve got our data constructed, what we’re going to do is design our section-headerView in our TableView, basically adding a click event to the HeaderView, and then responding to it in our MainViewController, There are many different solutions (delegate, block, notification). I’m using a block, which is relatively simple. The tap event code is as follows:

If (_isOpen) {[UIView animateWithDuration:0.3 animations:^{_imageview.transform = CGAffineTransformRotate(_imageView.transform, -M_PI / 2); }]; self.closeblock(self.section); }else{[UIView animateWithDuration:0.3 animations:^{_imageview.transform = CGAffineTransformRotate(_imageView.transform, M_PI / 2); }]; self.openblock(self.section); } self.isOpen = ! self.isOpen;Copy the code

Back in our MainViewController:

HeaderView *headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
headerView.nameLabel.text = model.name;
headerView.section = section;

__weak typeof(self) weakself = self;
headerView.openblock =^(NSInteger secion){
    [weakself openSection:section];
};
headerView.closeblock = ^(NSInteger section){
    [weakself closeSection:section];
};
Copy the code

The expansion method is as follows:

BaseDataModel *model = self.dataArray[section]; model.isOpen = ! model.isOpen; NSMutableArray *indexArray = [NSMutableArray arrayWithCapacity:10]; for (int i = 0; i < model.dataArray.count; i++) { NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:section]; [indexArray addObject:indexpath]; } [self.tableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];Copy the code

The closing method is:

BaseDataModel *model = self.dataArray[section]; model.isOpen = ! model.isOpen; NSMutableArray *indexArray = [NSMutableArray arrayWithCapacity:10]; for (int i = 0; i < model.dataArray.count; i++) { NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:section]; [indexArray addObject:indexpath]; } [self.tableView deleteRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];Copy the code

NumberOfRowsInSection since the corresponding datasource changes when you delete or add data, return numberOfRowsInSection:

BaseDataModel *model = self.dataArray[section];
if (model.isOpen) {
    return model.dataArray.count;
}else{
    return 0;
}
Copy the code

Because tableView has its own reuse mechanism, sectionHeaderView can also be reused, so if the data source is not set properly, it will be messy. In MVC design mode, it is used to control the state of the View, so it can write the parameters of the control state in init initialization:

- (instancetype)initWithFrame:(CGRect)frame IsOpen:(BOOL)isOpen {
 if (self = [super initWithFrame:frame]) {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.nameLabel];
    [self addSubview:self.imageView];
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOpen)]];
    self.isOpen = isOpen;
    if (self.isOpen) {
       _imageView.transform =  CGAffineTransformRotate(_imageView.transform, M_PI / 2);
    }
 }
 return self;
}
Copy the code

In this case, a simple expanded and contracted TableView is done. The demo address: https://github.com/ioscick/Extand-TableView

Welcome to read, hope to help you, if there are incorrect places can also discuss ~ thanks.