2015-05-27 26 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

相關問題