简体   繁体   English

使用Swift在表视图的自定义单元格下添加图例

[英]Add legend under Custom Cell in Table View with Swift

I'm currently trying to replicate the array you can see below in the picture: 我目前正在尝试复制您可以在下面的图片中看到的数组:

在此处输入图片说明

I've created a custom cell class so I can display a label and a switch button. 我创建了一个自定义单元格类,因此可以显示标签和切换按钮。 The part I have no idea about is how to display the legend below every cell. 我不知道的部分是如何在每个单元格下方显示图例。

Here is my code at the moment: 这是我目前的代码:

 var options = ["Solstice", "Equinox", "Enable Snapshot"]

 public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return options.count
}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
    let switchView = UISwitch(frame: CGRect.zero)
    cell.addSubview(switchView)
    cell.accessoryView = switchView
    cell.nameLabel.text = options[indexPath.row]
    return cell
}

Could the legend be another custom cell, with a different style? 图例可能是另一个具有不同样式的自定义单元格吗? What would be the best way to do it? 最好的方法是什么?

Setup your table view with a grouped style instead of plain. 使用分组样式而不是普通样式来设置表格视图。 Put each row is in its own section. 将每一行放在其自己的部分中。

Use a section footer title for each of the legends. 为每个图例使用节的页脚标题。 This is done with the func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? 这是通过func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? data source method. 数据源方法。

There are two options:- 有两种选择:

1) 1)

  var options = [ {"title":"Solstice", "description" :"legend" }, {"title":"Equinox", "description" :"legend"} , {"title":"Enable Snapshot", "description" :"legend"}]

Then, You can create the label,switch and legend in same cell itself. 然后,您可以在同一单元格本身中创建标签,开关和图例。 For the label in legend should be given number of lines 0, and no constant height so that label increases height based on text similar to AutoLayout to dynamically size UILabel Height and Width 对于图例中的标签,行号应为0,且行号不得恒定,以使标签根据类似于AutoLayout的文本增加高度, 以动态调整UILabel Height和Width的大小

2) Create view with switch as section headers, and legend as row inside each header // this is commonly used when there are more than one row for each section, since your use case has only one row (legend), using single cell will be easier for implementation 2)创建带有switch作为部分标题的视图,并在每个标题内的行作为图例// //当每个部分有多个行时,通常使用此视图,因为用例只有一行(传说),使用单个单元格将易于实施

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM