UITableViewCell with xib

后端 未结 1 1651
离开以前
离开以前 2021-02-07 08:59

I\'m developing an app for iOS which use the xib files.

Usually I use Storyboard and I don\'t know how to set up a UITableViewCell with xib files. When I m

相关标签:
1条回答
  • 2021-02-07 09:07

    Firstly, you need to create the class for the customCell which inherits from UITableViewCell. Now, add the properties you want to have in your customCell. In this example I added cellImage and cellLabel.

    @property (nonatomic, strong) IBOutlet UILabel *cellLabel;
    @property (nonatomic, strong) IBOutlet UIImageView *cellImageView;
    

    After that you need to link the UILabel and the UIImageView from the CustomCell to the Nib.

    You need to add:

    - (void)viewDidLoad 
    {
        ....
        [self.tableView registerNib:[UINib nibWithNibName:@"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];
        .....
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"CustomCellReuse";
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        // Configure the cell...
        [cell.cellImageView setImage:[UIImage imageNamed:@"whatever"]];
        [cell.cellLabel setText = @"whatever"];
        return cell;
    }
    
    0 讨论(0)
提交回复
热议问题