you can achieve this in two way First creating 2 prototype cells one for Header view and other for details view and Second one prototype cells one for details view and create your header in viewForHeaderInSection
for each section.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
so it's easy for you to do with first one here is the code.
import UIKit
public struct Section {
var name: String
var collapsed: Bool
public init(name: String, collapsed: Bool = false) {
self.name = name
self.collapsed = collapsed
}
}
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var sampleData: [Section] = [Section(name: "Header 1"),Section(name: "Header 2"),Section(name: "Header 3")]
After setting up data expand collapse uitableviewcell
//
// MARK: - View Controller DataSource and Delegate
//
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sampleData[section].collapsed ? 2 : 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return sampleData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Header
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "header")!
return cell
}
// Cell
let cell = tableView.dequeueReusableCell(withIdentifier: "detailed")!
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
let collapsed = !sampleData[indexPath.section].collapsed
// Toggle collapse
sampleData[indexPath.section].collapsed = collapsed
self.tableView.reloadSections([indexPath.section], with: .automatic)
}
}
