簡體   English   中英

ios tableViewCell.xib文件生成多個單元格

[英]ios tableViewCell.xib file generate more than one cell

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FIDCELL"];
    return cell;
}

此代碼塊有效,但是當我在FriendTableViewCell.xib中添加新單元格時,實際上無法正常工作,因此無法調用新單元格。 我怎么稱呼這個細胞或者這是可能的? 如果這個問題不清楚,我可以添加圖片...

**

我嘗試從同一.xib調用不同的單元格

**

圖片1

錯誤:

錯誤

我發現解決方案我生成新的.xib並在頁面加載兩者時調用

[self.tableView registerNib:[UINib nibWithNibName:@"FriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"FIDCELL"];
[self.tableView registerNib:[UINib nibWithNibName:@"Cell2" bundle:nil] forCellReuseIdentifier:@"FIDCELL2"];

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

    NSString *cellID = @"";
    if (indexPath.row % 2 == 0) {
        cellID = @"FIDCELL";
    } else {
        cellID = @"FIDCELL2";
    }
    FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    // Configure the cell...
    cell.nameLabel.text = self.namesArray[indexPath.row];
    cell.photoImageView.image = [UIImage imageNamed:self.photoNameArray[indexPath.row]];

    return cell;
}

該代碼部分完全起作用。

Okey首先,每個單元格都應具有其唯一標識符,因此,如果第一個單元格具有一個名為“ FIDCELL”的標識符,那么第二個單元格應該具有另一個名為“ FIDCELL2”的標識符,並從viewDidLoad中刪除以下行:

[self.tableView registerNib:[UINib nibWithNibName:@"FriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"FIDCELL"];

並將其添加到cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCell * cell ;
    if(condtion1)
    {
       cell = [tableView dequeueReusableCellWithIdentifier:firstCellIdentfier];
    }
    else if(condtion2)
    {
       cell = [tableView dequeueReusableCellWithIdentifier:secondCellIdentfier];
    }
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:yourNibName owner:self options:nil];
        if(condtion1)
        {
           //0 is the index of the first cell in the nib
           cell = [nib objectAtIndex:0];
        }
        else if(condtion2)
        {
           //1 is the index of the second cell in the nib .
           cell = [nib objectAtIndex:1] ; 
        }

    }
       //do other work 
   return cell ;
}

其中condtion1和condtion2是確定將選擇哪個單元的條件。

暫無
暫無

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

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