Register nib name for tableView

2020-07-02 08:50发布

问题:

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?

回答1:

-(void)viewDidLoad
{
   [super viewDidLoad];
   [self.tableView registerNib:[UINib nibWithNibName:@"cell" bundle:nil] 
   forCellReuseIdentifier:@"cell"];
}


回答2:

Apple provided register nib method for UITableView after IOS 5 Please check class reference http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

Ex:

     In view did load you can register nib for UITableView like below
     [tableView registerNib:[UINib nibWithNibName:@"nibName" bundle:nil] forCellReuseIdentifier:@"identifienName"];

      In cellForRowAtIndexPath
      cell =  [tableView dequeueReusableCellWithIdentifier:@"identifienName"];


回答3:

 (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.



回答4:

For Swift

tableViewSubCategory.register(UINib(nibName: "", bundle: nil), forCellReuseIdentifier: "")


回答5:

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


回答6:

static NSString *cellIdentifier = @"cell";
if (tableView ==tableview1) 
{
    ContactCustom *cell1=(ContactCustom *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];        
    if (cell1 == nil) 
    {
        cell1 = [tableview dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 

    }
}


回答7:

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



回答8:

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


回答9:

UINib *cellNib = [UINib nibWithNibName:@"Custom_cellTableViewCell" bundle:nil]; [self.tableView registerNib:cellNib forCellReuseIdentifier:@"Custom_cellTableViewCell"];

This code is working fine