2017-08-01 118 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