简体   繁体   中英

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. Which the sectionData is listed as a var inside of a structure(cellData).

 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM