簡體   English   中英

如何將自定義 UITableViewCell 添加到 xib 文件中

[英]How to add a custom UITableViewCell to a xib file objective-c

我想在視圖控制器中為 tableviewcell 加載我的自定義 xib。我無法在 tableview 中加載我的 xib

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    CustomCell *cell = (CustomCell *)[tableView  dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
    if (cell == nil) {
           [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
           cell = _customCell;
           _customCell = nil;
    }        

    cell.topLabel.text = @"I am on top";
    cell.bottomLabel.text = @"and I'm on the bottom";

    return cell;     
}

我無法將我的自定義單元格從 xib 加載到 tableview 中。 請幫幫我。

有兩種選擇:

選項 1

首先,您可以在 viewDidLoad 中注冊自定義單元格

UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:@"cell"];

然后在 TableView DataSource 方法的cellForRowAtIndexPath中:

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.lblName.text = @"Tejas";

選項 2

如果您沒有在 viewDidLoad 中注冊單元格,則必須在 TableView DataSource 方法的cellForRowAtIndexPath中執行以下操作:

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
if(cell == nil)
   cell = nibs[0];
cell.lblName.text = @"Tejas";
return cell;

使用以下方法在 UITableView 中注冊單元格(XIB 單元格):

[self.tableView registerNib:[UINib nibWithName:nibName bundle:nil] forCellReuseIdentifier:@"CustomCell"];

然后,您可以使用用於注冊單元的標識符使單元出列。

你可以在 Tableview cellforrowAtIndexPath 方法中這樣做,

static NSString *CellIdentifier = @"CellData";

NSArray *arrData = [[NSBundle mainBundle]loadNibNamed:@"cellTaskDetail" owner:nil options:nil];

cellTaskDetail *cell = [[cellTaskDetail alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell = [arrData objectAtIndex:0];

return cell;

它的工作正常......做吧

NSString *simpleTableIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];   
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil)       
{       
    NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];   
    cell = [nibArray objectAtIndex:0];    
}

cell.topLabel.text = @"I am on top";
cell.bottomLabel.text = @"and I'm on the bottom";

return cell;     

使用loadNibNamed簡單解決方案

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"CellIdentifier";
    YourCustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {     cell = [[[NSBundle mainBundle] loadNibNamed:@"YourCustomeCell" owner:self options:nil] objectAtIndex:0];
    }

    // do code with cell here

    return cell;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM