Register nib name for tableView

2020-07-02 08:43发布

static NSString *cellIdentifier = @"cell";
if (tableView ==tableview1) 
{
    ContactCustom *cell1=(ContactCustom *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];        
    if (cell1 == nil) 
    {
        [[NSBundle mainBundle] loadNibNamed:@"ContactCustom" owner:self options:nil];
        cell1 = contactCustom;
    }
}

How to register nib name in viewDidLoad method before calling cellForRowAtIndex method?

9条回答
男人必须洒脱
2楼-- · 2020-07-02 08:47

See here: http://mrmaksimize.com/Custom-UITableViewCell-With-NIB/

 - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.tableView registerNib:[UINib nibWithNibName:@"EXCustomCell"
                             bundle:[NSBundle mainBundle]]
             forCellReuseIdentifier:@"CustomCellReuseID"];
    }

Later in code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCellReuseID";
    EXCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    [cell.cellItemImage setImage:[UIImage imageNamed:@"glyphicons_428_podium"]];
    [cell.cellItemLabel setText = @"Mr Burns."];
    return cell;
}
查看更多
做自己的国王
3楼-- · 2020-07-02 08:47

Registers a nib object containing a cell with the table view under a specified identifier.

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
Parameters

nib A nib object that specifies the nib file to use to create the cell. This parameter cannot be nil. identifier The reuse identifier for the cell. This parameter must not be nil and must not be an empty string.

This doc can help you a lot

查看更多
戒情不戒烟
4楼-- · 2020-07-02 08:49
 (void)viewDidLoad
{
   [super viewDidLoad];
    [_tbl_setpaper registerNib:[UINib nibWithNibName:@"SetPaperCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
}

Just put oneline in your viewdidload, you must have table outlet,cell identifier,cell class.

查看更多
▲ chillily
5楼-- · 2020-07-02 08:52

For Swift

tableViewSubCategory.register(UINib(nibName: "", bundle: nil), forCellReuseIdentifier: "")
查看更多
迷人小祖宗
6楼-- · 2020-07-02 09:08
static NSString *cellIdentifier = @"cell";
if (tableView ==tableview1) 
{
    ContactCustom *cell1=(ContactCustom *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];        
    if (cell1 == nil) 
    {
        cell1 = [tableview dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 

    }
}
查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-07-02 09:09
// First of all Declare an IBOutlet for your customCell in Your View Controller
IBOutlet ScoreCell *scoreCell;
// Synthesize it.
// Assign your view controller class to File Owner of your custom cell (Eg. File Owner of ScoreCell.xib)
// Then Assign Reference Outlet of ScoreCell.xib with Object 'scoreCell'
// Finally Create your custom Cell as follow :

ScoreCell *cell = (ScoreCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
查看更多
登录 后发表回答