[英]How can I access a var inside of a structure to count the number of items listed in the var's array?
I need help being able to display in a string format within the each sections title of the table view the number of listed (Cells) inside the sectionData array. 我需要能够在表视图的每个节标题中以字符串格式显示sectionData数组中列出的(单元格)数量的帮助。 Which the sectionData is listed as a var inside of a structure(cellData). 其中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 Swift 4:结构化数组显示在tableView中
iPhoneX Build: Display of Sections in tableview iPhoneX构建:在表视图中显示节
iPhoneX Build: Display of Sections and Cells in tableView when clicked iPhoneX版本:单击时在tableView中显示节和单元格
This is the easiest way i can think of. 这是我能想到的最简单的方法。 You would need two extra variables - one for get ting the count, another for the count appended title. 您将需要两个额外的变量-一个用于获取计数,另一个用于附加计数的标题。 If you don't mind building the string to be displayed yourself, you can skip the second one. 如果您不介意构建要自己显示的字符串,则可以跳过第二个字符串。
struct cellData {
var opened = Bool()
var title = String()
var sectionData = [String]()
var count: Int {
get {
return sectionData.count
}
}
// This variable is for convenience
var titleWithCount: String {
get {
return "\(title) (\(count) Cells)" // Format it as you require
}
}
}
Use the titleWithCount
variable when populating the section title. 填充节标题时,请使用titleWithCount
变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.