为tableView注册nib名称

时间:2013-03-12 12:03:49

标签: objective-c xcode uitableview

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

如何在调用viewDidLoad方法之前在cellForRowAtIndex方法中注册nib名称?

10 个答案:

答案 0 :(得分:28)

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

答案 1 :(得分:2)

Apple在IOS 5之后为UITableView提供了注册nib方法 请检查课程参考 http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

例如:

     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"];

答案 2 :(得分:2)

 (void)viewDidLoad
{
   [super viewDidLoad];
    [_tbl_setpaper registerNib:[UINib nibWithNibName:@"SetPaperCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
}

只需在你的viewdidload中放入线,你必须有表插座,单元标识符,单元类。

答案 3 :(得分:2)

对于Swift

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

答案 4 :(得分:0)

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

    }
}

答案 5 :(得分:0)

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

答案 6 :(得分:0)

使用指定标识符下的表视图注册包含单元格的nib对象。

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

<强>笔尖 一个nib对象,它指定用于创建单元的nib文件。此参数不能为零。 的标识符 单元的重用标识符。此参数不能为nil,且不能为空字符串。

doc可以帮到你很多

答案 7 :(得分:0)

见这里:http://mrmaksimize.com/Custom-UITableViewCell-With-NIB/

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

稍后在代码中:

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

答案 8 :(得分:0)

  

UINib * cellNib = [UINib nibWithNibName:@&#34; Custom_cellTableViewCell&#34;捆绑:无];      [self.tableView registerNib:cellNib forCellReuseIdentifier:@&#34; Custom_cellTableViewCell&#34;];

此代码工作正常

答案 9 :(得分:0)

对于目标C

//First TableView Declaration:
self.customTableView = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];

//Then Nib Registration Registration:
[self.customTableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil]  forCellReuseIdentifier:@"customCellIdentifier"];

//Then Adding Tableview to self.view
[self.view addSubview: _customTableView];