OGeek|极客世界-中国程序员成长平台

标题: ios - 如何访问结构内的 var 以计算 var 数组中列出的项目数? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:35
标题: ios - 如何访问结构内的 var 以计算 var 数组中列出的项目数?

我需要帮助才能在表格 View 的每个部分标题中以字符串格式显示 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
        }
    }

//

}


Best Answer-推荐答案


这是我能想到的最简单的方法。您将需要两个额外的变量 - 一个用于 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 变量。

关于ios - 如何访问结构内的 var 以计算 var 数组中列出的项目数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52347863/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4