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

标题: ios - swift : Save & Show selected indexPath in static Table View [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:48
标题: ios - swift : Save & Show selected indexPath in static Table View

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

 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..<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: "reload"), object: nil)

            }
        }
    }
}

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

我已阅读 this用于显示复选标记但不保存状态。我已阅读 this也是,但它也一样。任何建议/答案都会对我有所帮助。提前致谢



Best Answer-推荐答案


func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath == lastSelected {
        cell.accessoryType = .Checkmark
    }
}

关于ios - swift : Save & Show selected indexPath in static Table View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44381738/






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