菜鸟教程小白 发表于 2022-12-11 19:48:43

ios - swift : Save & Show selected indexPath in static Table View


                                            <p><p>我的静态表格 View 有点问题。我有一个 PopOver,它显示为我的网格的一个选项。我想保存我的静态 TableView 的状态(indexPath),但它似乎不起作用,下面是我的代码片段:</p>

<pre><code> override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var currSelected: IndexPath?

    let section = indexPath.section
    let numberOfRows = tableView.numberOfRows(inSection: section)
    for row in 0..&lt;numberOfRows {
      if let cell = tableView.cellForRow(at: NSIndexPath(row: row, section: section) as IndexPath) {
            cell.accessoryType = row == indexPath.row ? .checkmark : .none
            tableView.deselectRow(at: indexPath, animated: false)
            currSelected = indexPath

            if section == 2 {
                tableView.deselectRow(at: indexPath, animated: false)
                cell.accessoryType = .none
            }
            else {
                delegate?.option(lastSelected: currSelected!)
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: &#34;reload&#34;), object: nil)

            }
      }
    }
}
</code></pre>

<p>我正在尝试使用 <code>delegate</code> 将 <code>lastSelected</code>(最后一个 indexPath)扔到前一个 Controller 并将其发送回 Pop OverController ,我得到了它。但我不知道使用这个 lastSelected 显示复选标记。 <code>prepare for cell at</code> 函数需要一个重用单元的标识符,但我不使用它。 </p>

<p>我已阅读 <a href="https://stackoverflow.com/questions/29407723/checkmark-on-static-cells-uitableview" rel="noreferrer noopener nofollow">this</a>用于显示复选标记但不保存状态。我已阅读 <a href="https://blog.apoorvmote.com/how-to-use-static-cells-to-save-app-settings/" rel="noreferrer noopener nofollow">this</a>也是,但它也一样。任何建议/答案都会对我有所帮助。提前致谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath == lastSelected {
      cell.accessoryType = .Checkmark
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios -swift: Save &amp; Show selected indexPath in static Table View,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44381738/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44381738/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - swift : Save &amp; Show selected indexPath in static Table View