简体   繁体   English

如何访问结构内部的var以计算var数组中列出的项目数?

[英]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.

相关问题 从 class 中的枚举访问 var - access var from enum inside a class 如何使用 section 参数遍历集合内的每个文档以访问子集合的计数? - How can I use the section parameter to iterate over each document that's inside of a collection to access the count of a subcollection? 如何传递var的值以在TextView中打印 - How can I pass the value of a var to print in my TextView 如何在 Swift 和 Realm 中使用惰性变量? - How can I use a lazy var in Swift and Realm? 如何在var名称中使用#。 JSON,iOS,Swift - How can I use # in a var name. JSON, iOS, Swift 在我微小的Objective-C类设计上需要帮助。 如何访问实例var? - Need help on my tiny objective-c class design. How can I access my instance var? 如何获取UITableView内的UICollectionView内的数组的数组,并还返回正确数量的项? - How to get an array of arrays inside of UICollectionView that's inside a UITableView and also return the correct number of items? 如何访问private / var文件夹中的文件? - How to access a file in a private/var folder? 在另一个函数中访问 var - access var in another function 如何计算对象数组中keyValues的总数 - how can I count total number of keyValues in array of object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM