[英]Swift UITableView with Custom Cell and Multiple Sections array data
My Scenario, I am trying to create UITableView
with custom cell and multiple sections from single array. 我的方案是,我尝试使用自定义单元格和单个数组的多个节创建
UITableView
。 Here, I need to show different titles in sections. 在这里,我需要在各节中显示不同的标题。 How to do this?
这个怎么做? I used below code but not able to get clear understanding.
我使用下面的代码,但无法清楚地理解。
My Code below 我的下面的代码
var data = [["1","2","3"], ["4","5"]]
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return data.count
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20
}
There can be 2 cases around it, 周围可能有2种情况,
In case you simply want to add String
titles to each section
, implement UITableViewDataSource's
tableView(_: titleForHeaderInSection:)
method. 如果只想向每个
section
添加String
标题 ,请实现UITableViewDataSource's
tableView(_: titleForHeaderInSection:)
方法。
And if you want to give a custom view
for each section
, implement UITableViewDelegate's
tableView(_:viewForHeaderInSection:)
method. 而且,如果要为每个
section
提供自定义view
,请实现UITableViewDelegate's
tableView(_:viewForHeaderInSection:)
方法。
Here is an example with tableView(_: titleForHeaderInSection:)
, 这是
tableView(_: titleForHeaderInSection:)
的示例,
class VC: UIViewController, UITableViewDataSource {
let data = [["1","2","3"], ["4","5"]]
let sectionNames = ["This is Sec-1", "And this is Sec-2"]
func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.textLabel?.text = data[indexPath.section][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionNames[section]
}
}
Do implement tableView(_:heightForHeaderInSection:)
in case you need custom height for each section
. 如果需要为每个
section
自定义高度,请实现tableView(_:heightForHeaderInSection:)
。
Screenshot: 屏幕截图:
Edit: 编辑:
Use accessoryType
as .checkmark
for cell selection. 使用
accessoryType
作为.checkmark
进行单元格选择。
Create a custom UITableViewCell
and override setSelected(_:animated:)
method in that like so, 创建一个自定义
UITableViewCell
并override setSelected(_:animated:)
方法,就像这样,
class CustomCell: UITableViewCell {
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.accessoryType = selected ? .checkmark : .none
}
}
Set the reuseIdentifier
of CustomCell
in xib
as cell
. 设置
reuseIdentifier
的CustomCell
在xib
的cell
。 Also, update the tableView(_:cellForRowAt:)
method to dequeue CustomCell
instance. 此外,更新
tableView(_:cellForRowAt:)
方法以使CustomCell
实例出队。 I updated that in the code above. 我在上面的代码中进行了更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.