菜鸟教程小白 发表于 2022-12-11 20:35:54

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


                                            <p><p>我需要帮助才能在表格 View 的每个部分标题中以字符串格式显示 sectionData 数组中列出的(单元格)的数量。其中sectionData被列为结构(cellData)内的var。 </p>

<p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>import UIKit

struct cellData
{
   
    var opened = Bool()
    var title = String()
    var sectionData = ()

   
}



class TableViewController: UITableViewController {

    var tableViewData = ()
   
    override func viewDidLoad()
    {
      super.viewDidLoad()
      
      tableViewData = ),
                         cellData(opened: false, title: &#34;Tuesday, September 11, 2018&#34;, sectionData: [&#34;Cell1&#34;, &#34;Cell2&#34;, &#34;Cell3&#34;]),
                         cellData(opened: false, title: &#34;Wednesday, September 12, 2018&#34;, sectionData: [&#34;Cell1&#34;, &#34;Cell2&#34;, &#34;Cell3&#34;]),
                         cellData(opened: false, title: &#34;Thursday, September 13, 2018&#34;, sectionData: [&#34;Cell1&#34;, &#34;Cell2&#34;, &#34;Cell3&#34;]),
                         cellData(opened: false, title: &#34;Friday, September 14, 2018&#34;, sectionData: [&#34;Cell1&#34;, &#34;Cell2&#34;, &#34;Cell3&#34;])]
    }

    override func numberOfSections(in tableView: UITableView) -&gt; Int
    {
      return tableViewData.count
    }
   
//
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int
    {
      if tableViewData.opened == true
      {
            return tableViewData.sectionData.count + 1
      }
      else
      {
            return 1
      }
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell
    {
   let dataIndex = indexPath.row - 1
      
   if indexPath.row == 0
   {
      guard let cell = tableView.dequeueReusableCell(withIdentifier: &#34;cell&#34;) else {return UITableViewCell()}
      cell.textLabel?.text = tableViewData.title
      
      return cell
   }
      

   else
   {
      guard let cell = tableView.dequeueReusableCell(withIdentifier: &#34;cell&#34;) else {return UITableViewCell()}
      cell.textLabel?.text = tableViewData.sectionData
      
      return cell
   }
    }
//
   
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
      if tableViewData.opened == true
      {
            tableViewData.opened = false
            let sections = IndexSet.init(integer: indexPath.section)
            tableView.reloadSections(sections, with: .none)// play around with this
      }
      
      else
      {
            tableViewData.opened = true
            let sections = IndexSet.init(integer: indexPath.section)
            tableView.reloadSections(sections, with: .none)// play around with this
      }
    }

//

}</code></pre>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这是我能想到的最简单的方法。您将需要两个额外的变量 - 一个用于 <strong>get</strong> 计数,另一个用于计数附加标题。如果您不介意自己构建要显示的字符串,可以<em>跳过</em>第二个。</p>

<pre><code>struct cellData {
    var opened = Bool()
    var title = String()
    var sectionData = ()

    var count: Int {
      get {
            return sectionData.count
      }
    }
    // This variable is for convenience
    var titleWithCount: String {
      get {
            return &#34;\(title) (\(count) Cells)&#34; // Format it as you require
      }
    }
}
</code></pre>

<p>在填充部分标题时使用 <code>titleWithCount</code> 变量。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何访问结构内的 var 以计算 var 数组中列出的项目数?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52347863/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52347863/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何访问结构内的 var 以计算 var 数组中列出的项目数?