2017-08-01 180 views
0

試圖瞭解如何使用自定義單元格的3個部分製作自定義UItableview,如下所示。iOS自定義UItableviewcell部分

  Sensor Info 
====================== 
Sensor 1: $Value_1 
Sensor 2: $Value_2 
Sensor 3: $Value_3 
Sensor 4: $Value_4 
Sensor 5: $Value_5 
Sensor 6: $Value_6 

      Errors 
=================== 
Error 1 : $error1 
Error 2..: $error2.. 

     Another Section 
======================== 
Error 1 : $error1 
Error 2..: $error2.. 

一個建議可能是將所有內容添加到一個數組,然後填充表,但我需要每個段加載後。每個片段內容都是在按下按鈕後從視圖控制器類中的方法生成的。所以傳感器信息,錯誤,其他部分和更多,如果我打算以後再添加。

我很難試圖掌握如何爲每個部分建立一個不同的行集合表。我想製作所有傳感器數據的自定義單元格和字幕單元格或右側的細節。

+0

如果你是罰款庫比請嘗試STCollapseTableView ... –

回答

1

您可以擁有儘可能多的來源和部分你想要你的表。

您甚至可以爲每個部分創建一個原型單元格。

只是驗證你在哪裏,處理它,如:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 44 
} 

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

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    if (section == 0) { 
     return "Title 1" 
    } 
    if (section == 1) { 
     return "Title 2" 
    }   
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if section == 0 { 
     return contactPhones.count 
    } else if section == 1 { 
     return contactEmails.count 
    } else { 
     return 1 
    } 
} 

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cellIdentifier: String = "Cell" 
    var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as UITableViewCell! 
    if cell == nil { 
     cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: cellIdentifier) 
    } 

    if indexPath.section == 0 { 
     cell?.textLabel?.text = contactPhones[indexPath.row]["phoneNumber"].stringValue 
     cell?.imageView?.image = UIImage(flagImageWithCountryCode: contactPhones[indexPath.row]["phoneCountryCode"].stringValue) 
    } else if indexPath.section == 1 { 
     cell?.textLabel?.text = contactEmails[indexPath.row]["emailAddress"].stringValue 
    } 

    return cell! 
} 
+0

你有這個代碼的Objective-C的版本。我還沒有快速學習。 – 3rdeye7

+0

否,但表視圖中的委託方法是相同的,您必須處理其中的'if'才能達到您的目標 – GIJOW

+0

從代碼判斷,您是否將三個不同的表視圖添加到故事板中的視圖控制器? – 3rdeye7