【问题标题】:How can I access a var inside of a structure to count the number of items listed in the var's array?如何访问结构内的 var 以计算 var 数组中列出的项目数?
【发布时间】: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


    【解决方案1】:

    这是我能想到的最简单的方法。您将需要两个额外的变量 - 一个用于 get 计数,另一个用于计数附加标题。如果您不介意自己构建要显示的字符串,您可以跳过第二个。

    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
            }
        }
    }
    

    在填充部分标题时使用titleWithCount 变量。

    【讨论】:

    • 谢谢!我现在理解了获取和返回计数值背后的概念。我对您的唯一问题是如何编写或格式化 cellData 的变量结构以在我的 viewDidLoad 部分中使用“titleWithCount”。我想在标题字符串旁边的部分标题中显示下面列出的单元格数。例如:“2018 年 9 月 10 日,星期一(3 个单元格)”。
    • @MichaelEdlin 我已经在最后一行解释过了。检查编辑。
    • 没关系!一旦我看到您使用 titleWithCount 填充部分标题的最后一条语句,我就明白了。我不得不用 titleWithCount 更改 [indexPath.section].title。谢谢!
    猜你喜欢
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 2022-01-10
    • 1970-01-01
    • 2021-08-16
    相关资源
    最近更新 更多