Custom uitableviewcell and uitableviewcell

Source: Internet
Author: User

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;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.