【发布时间】:2018-09-15 18:52:24
【问题描述】:
我需要帮助才能在表格视图的每个部分标题中以字符串格式显示 sectionData 数组中列出的(单元格)数量。其中sectionData被列为结构(cellData)内的var。
import UIKit
struct cellData
{
var opened = Bool()
var title = String()
var sectionData = [String]()
}
class TableViewController: UITableViewController {
var tableViewData = [cellData]()
override func viewDidLoad()
{
super.viewDidLoad()
tableViewData = [cellData(opened: false, title: "Monday, September 10, 2018", sectionData: ["Cell1", "Cell2", "Cell3"]),
cellData(opened: false, title: "Tuesday, September 11, 2018", sectionData: ["Cell1", "Cell2", "Cell3"]),
cellData(opened: false, title: "Wednesday, September 12, 2018", sectionData: ["Cell1", "Cell2", "Cell3"]),
cellData(opened: false, title: "Thursday, September 13, 2018", sectionData: ["Cell1", "Cell2", "Cell3"]),
cellData(opened: false, title: "Friday, September 14, 2018", sectionData: ["Cell1", "Cell2", "Cell3"])]
}
override func numberOfSections(in tableView: UITableView) -> Int
{
return tableViewData.count
}
//
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
if tableViewData[section].opened == true
{
return tableViewData[section].sectionData.count + 1
}
else
{
return 1
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let dataIndex = indexPath.row - 1
if indexPath.row == 0
{
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
cell.textLabel?.text = tableViewData[indexPath.section].title
return cell
}
else
{
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {return UITableViewCell()}
cell.textLabel?.text = tableViewData[indexPath.section].sectionData[dataIndex]
return cell
}
}
//
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if tableViewData[indexPath.section].opened == true
{
tableViewData[indexPath.section].opened = false
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)// play around with this
}
else
{
tableViewData[indexPath.section].opened = true
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)// play around with this
}
}
//
}
Swift 4: Structured array to display in tableView
iPhoneX Build: Display of Sections in tableview
iPhoneX Build: Display of Sections and Cells in tableView when clicked
【问题讨论】:
标签: ios swift uitableview