Custom uitableviewcell and uitableviewcell
Create a subclass inherited from uitableviewcell
Customize the xib style and specify its reuse identifier
Configure cell data
# Import <UIKit/UIKit. h> @ interface CellStyleTwo: UITableViewCell // employee ID @ property (weak, nonatomic) IBOutlet UILabel * userCode; // call @ property (weak, nonatomic) IBOutlet UILabel * phoneNumber; /*** configure cell data */-(void) configureCellStyleTwo :( id) data; @ end
/*** Configure cell data ** @ param data */-(void) configureCellStyleTwo :( id) data {// if the data is a dictionary NSDictionary * dictionary = (NSDictionary *) data; if (dictionary) {self. userCode. text = dictionary [@ "usercode"]; self. phoneNumber. text = dictionary [@ "userTel"] ;}}
If you want to rewrite the initialization, you need to use the method. If you want to use it in this way, you do not need
[self.tableView registerNib:[UINib nibWithNibName:@"CellStyleTwo" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CellStyleTwo"];
In this way, the system directly finds the specified resource and does not use the default creation method.
Below we need
[self.tableView registerClass:[CellStyleTwo class] forCellReuseIdentifier:@"CellStyleTwo"];
-(nonnull instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{ self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]; return [[[NSBundle mainBundle] loadNibNamed:@"CellStyleTwo" owner:self options:nil] firstObject];}
This method only specifies the registered class, and the system will create it in the default method, but it is not what we expected, so we need to rewrite the initialization.
However, cell reuse is the same.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell* uiTableViewCell= [tableView dequeueReusableCellWithIdentifier:self.nsStringIdentifier forIndexPath:indexPath]; self.blockConfigureCell(uiTableViewCell,self.nsArrayModels[indexPath.row]); tableView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero]; return uiTableViewCell;}