2015-05-27 28 views
0

我有一个TableView,我已经为该TableView创建了自定义单元格,带有图像,标签..等等,但是我希望一个单元格与其他单元格不同,从后端获取他们的数据)。 那么如何在同一个TableView中创建2种不同类型的单元格,以及如何安排它们,例如在2个其他单元格之间放置1种单元格?UITableView与3个自定义单元,但1个不同

编辑:我的UITableView类:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let myCell: CellTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! CellTableViewCell 

    let otherCell: OtherCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("otherCell", forIndexPath: indexPath) as! OtherCellTableViewCell 
    // Configure the cell... 

    return myCell 
} 

回答

1

如果您使用的是故事板,只需将另一UITableViewCell到表视图用作原型。给它一个独特的和子类(如果需要的话)。在-tableView:cellForRowAtIndexPath:中,使您当前正在出队的单元或这个新单元出列。

如果你不使用一个故事板,你需要要么 -registerClass:forHeaderFooterViewReuseIdentifier:-registerNib:forHeaderFooterViewReuseIdentifier:使可用表视图中的其他单元格样式。

这是你的代码的修改版本,应该工作:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if condition { 
     let myCell: CellTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! CellTableViewCell 
     // Configure the cell... 
     return myCell 
    } 
    else { 
     let otherCell: OtherCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("otherCell", forIndexPath: indexPath) as! OtherCellTableViewCell 
     // Configure the cell... 
     return otherCell 
    } 
} 
+0

我已经有一个自定义单元格“的tableView:的cellForRowAtIndexPath:”如果我加了一个又一个,其中一个将被退回?检查OP。 – Abdou023

+0

你得到了你指定的reuseIdentifier。现在它是具有“myCell”标识符的那个。给其他单元格一个不同的标识符,如“myOtherCell”。在选择单元格时,可以在Interface Builder的属性窗格中指定它。 –

+0

检查OP,我编辑了该功能,但现在它表现得很奇怪。当我返回一个单元格时,另一个单元格显示出来,如果我点击它,它会变成另一个单元格。问题是我只能返回其中之一。 – Abdou023

相关问题