вы можете достичь этого двумя способами Сначала , создав 2 прототипа ячейки, одну для просмотра заголовка и другую для просмотра подробностей, и Второй одну ячейку прототипа, одну для просмотра сведений, и создайте свой заголовок вviewForHeaderInSection
для каждого раздела.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
, поэтому вам будет легко с первым здесь ввести код.
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")]
Посленастройка данных развернуть свернуть 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)
}
}
![enter image description here](https://i.stack.imgur.com/iBECO.png)