Current applications are complicated. A tableView list will have a variety of cells. If all cells are processed in the controller, the effect will be achieved by using if and switch judgment, but the controller will become bloated. In fact, we can use the protocol to deal with the effect. 1. We can create a protocol file modelConfigProtocol.h

#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @protocol ModelConfigProtocol <NSObject> @optional // get cell reuse identifier -(NSString *)cellReuseIdentifier; // get cell height -(CGFloat)cellHeightWithindexPath:(NSIndexPath*)indexPath; / / / click event - (void) cellDidSelectRowAtIndexPath: (indexPath NSIndexPath *) other (_Nullable id) other; -(UITableViewCell*)tableView:(UITableView *)tableView reuseIdenyifier:(NSString *)reusedItentifier; @endCopy the code

2. Create a base class model basemodel. h, and comply with the protocol, implement the protocol method

#import "BaseModel.h"

@implementation BaseModel

- -(NSString *)cellReuseIdentifier {

    return @"";
}
 -(CGFloat)cellHeightWithindexPath:(NSIndexPath *)indexPath {

    return 0.0;
}
 -(void)cellDidSelectRowAtIndexPath:(NSIndexPath *)indexPath other:(id)other {

    
}
 -(UITableViewCell*)tableView:(NSString *)tableView reuseIdenyifier:(NSString *)reusedItentifier {

    UITableViewCell *cell = [[UITableViewCell alloc]init];
    
    return cell;
}
@end
Copy the code

3. Create a base Cell, and set the model that complies with the protocol to @property(nonatomic,strong) IDModel.

Subclass Cell rewrite model set method, base class Cell model set method implementation, do not need to do processing, real processing is put in the subclass Cell, according to the specific transaction specific analysis.

4. When creating a cell on a controller, you can write it without judging

-(void)viewDidLoad { [super viewDidLoad]; AModel *modelA = [[AModel alloc]init]; BModel *bmodel = [[BModel alloc]init]; CModel *cmodel = [[CModel alloc]init]; DModel *dmodel = [[DModel alloc]init]; self.dataArray = [NSMutableArray array]; [self.dataArray addObject:modelA]; [self.dataArray addObject:bmodel]; [self.dataArray addObject:cmodel]; [self.dataArray addObject:dmodel]; self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.tableView.dataSource = self; self.tableView.delegate = self; self.tableView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.tableView]; self.tableView.tableFooterView = [[UIView alloc]init]; [self.tableView reloadData]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count;  } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { id <ModelConfigProtocol>model = self.dataArray[indexPath.row]; BaseCell *cell = (BaseCell *)[model tableView:tableView reuseIdenyifier:[model cellReuseIdentifier]]; cell.model = model; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { id <ModelConfigProtocol>model = self.dataArray[indexPath.row]; return [model cellHeightWithindexPath:indexPath]; }Copy the code

Oc,swift Demo address github.com/suifumin/Mo…