简体   繁体   English

具有自定义单元格和多个节数组数据的Swift UITableView

[英]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种情况,

  1. In case you simply want to add String titles to each section , implement UITableViewDataSource's tableView(_: titleForHeaderInSection:) method. 如果只想向每个section添加String标题 ,请实现UITableViewDataSource's tableView(_: titleForHeaderInSection:)方法。

  2. 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, 创建一个自定义UITableViewCelloverride 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 . 设置reuseIdentifierCustomCellxibcell 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.

相关问题 具有多个节数组和行数据数组的Swift UITableview自定义单元格搜索实现 - Swift UITableview Custom Cell with multiple section array and row data array Search Implementation UITableView带有部分和Swift中的自定义对象 - UITableView with sections and custom Objects in Swift Swift-在UITableView中显示按日期排序的自定义对象数组 - Swift - Display array of custom objects with sections sorted by date in UITableView UITableView with Sections 在所有部分重复单元格数据 - UITableView with Sections Repeating cell data in all the sections 使用NSMutableArray数据填充UITableView自定义单元格-Swift - Populate UITableView Custom Cell with NSMutableArray data - Swift 如何在 UITableView iOS Swift 中加载数组值 int 单元而不创建多个自定义单元? - How to load array value int cell without creating multiple custom cell in UITableView iOS Swift? 如何创建带有节和分组单元格的自定义UITableView? - How to create a Custom UITableView with Sections and grouped cell? 如何快速将数组数据传递到自定义UITableView? - How to pass the array data to Custom UITableView in swift? 如何在 Swift 4 中的 UITableView 中制作多个级别的部分 - How to make multiple level sections in UITableView in Swift 4 使用 Realm 和 Swift 的具有多个部分的 UITableView - UITableView with Multiple Sections using Realm and Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM