2017-06-05 84 views
1

此問題跟從此:Use UICollectionViews to create dynamic and multiple features如何使用UITableViews與Swift一起創建靜態和動態行

我能夠創建一個顯示名稱,類似這樣的應用配方的圖像靜態細胞:

enter image description here

我在哪裏卡住正在創造它改變基於量的動態記錄即器皿或像下面的圖像的營養價值內的數據:

enter image description here

我知道如何上的tableView正常顯示數據行。但不知道如何將其嵌入到tableView中的一個部分。我試圖添加多個原型單元格並將它們分配給UITableViewCell的子類。然後我嘗試在我的cellForRow中使用if聲明,但這不是我的問題。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if indexPath.row == 0 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FirstCell 
     //set the data here 

     cell.recipeTitle.text = recipe.name 
     cell.imageView.image = UIImage(named: "url") 
     return cell 
    } 
    else if indexPath.row == 1 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! SecondCell 
     //set the data here 
     return cell 
    } 
    else { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell3", for: indexPath) as! ThirdCell 
     //set the data here 
     return cell 
    } 
} 

我也看了這個演示:https://medium.com/ios-os-x-development/ios-how-to-build-a-table-view-with-multiple-cell-types-2df91a206429,這是接近我想要實現,但我發現它很難去適應它。

如果有人能指導我如何最好地接近這個或一個很好的例子,那麼我會非常感激。

回答

1

首先,您不能在同一個tableView中擁有靜態單元格和動態單元格。那麼你如何解決這個問題?定義屬於它們的部分中的每個靜態單元格以及它們所屬部分中的動態單元格。然而,這看起來並不像你想要做的。你只需要在同一個tableView中的多個部分,每個部分有不同的數據列表

要做到這一點,你需要使用tableView(_:numberOfSections :)函數的部分數量。

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 3 
} 

然後,您可以(並且可能應該)通過在tableViewController初始化與標題的數組給每個那些部分的標題(假設這是你使用的是什麼,也可能只是一個的tableView)。

let headerTitles = ["Nutritional Values", "Utensils", "Ingredients"] 

然後使用的tableView(_:titleForHeaderInSection :)

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    if section < headerTitles.count { 
     return headerTitles[section] 
    } 
    return nil 
} 

現在你可以開始由部分定義你行。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell: UITableViewCell 

    if indexPath.section == 0 { 
     //Setup up the first row 
     if indexPath.row == 0 { 
      //I'm not sure where or how you defined First/SecondCell but this may not work depending on those two questions. 
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FirstCell     
      return cell 
     } else if indexPath.row == 1 { 
      let cell = Bundle.main.loadNibNamed("StaticCell", owner: self, options: nil)?.first as! StaticCell 
      return cell 
     } 

    } else if indexPath.section == 1 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! SecondCell 
     //setup cell1 n your storyboard to have a label with a tag # of 12(or whatever number you want to use) 
     //you also want an array with your utensil data accessible here 
     let label = cell.viewWithTag(12) as! UILabel 
     label.text = utensilNames[indexPath.row]    
      return cell 

    } else if indexPath.section == 2 { 
     let cellIngredients = tableView.dequeueReusableCell(withIdentifier: "Ingredients", for: indexPath) 

     tableView.deselectRow(at: indexPath, animated: true) 
     return cellIngreidents 
    } 
    cell = tableView.dequeueReusableCell(withIdentifier: "cell")! 
    return cell 
} 

這裏的要點是使用部分,然後行來分配您的數據。

只是爲了澄清部分0行0 - N將是您設置靜態行的位置。我發現最好使用子類化TableViewCell的XIB文件。 希望這有助於。

編輯所以我在看「靜態」單元格的方法是在第一部分中,xib是唯一放在你告訴它放置的地方。在上面的例子中,第二個單元格中的第一個部分是

+0

我很欣賞這個答案,我嘗試過你的例子,但我努力顯示第二和第三部分。當我運行它時,它只給了我第一部分的兩個副本。另外_FirstCell_和_SecondCell_是自定義TableViewCell的所有我的UI插座連接到。然後我在'cellForRowAt'處調用它們,即'cell.recipeLabel.text = recipe.name'。我用這個而不是標籤。最後,我的tableView也有一個_headerView_,它是UIView的子類。我用它來顯示食譜的圖像,這應該是我的tableView中的第一部分。 – Chace

+0

還有什麼意思是「定義它們所屬的部分中的每個靜態單元以及它們所屬部分中的動態單元。」目前我的tableView使用_Dynamic Prototypes_。 – Chace

+0

我的回答並不完整。我試圖提供一個模板供您作爲起點。它聽起來像你想要做的是使用一個xib和tableviewcell文件連接在一起,靜態單元格將在創建時調用。第二個評論的答案是「靜態」單元在xib和相關的swift文件中定義,然後通過cellForRowAt放置在tableview中: – returnVoid

相關問題