swift - 表格 View 单元格附件在 View 之外时不会更改

标签 swift

我有这段代码:

tableView.deselectRowAtIndexPath(indexPath, animated: true)
if let selected = self.lastSelected {
   tableView.cellForRowAtIndexPath(selected)?.accessoryType = .None
}
tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
self.lastSelected = indexPath

我允许我在选择的单元格上打勾,并删除之前的复选标记。我遇到的问题是,如果列表很大并且单元格不在 View 中,则选择新单元格时不会删除复选标记。

我尝试添加 tableview.reloaddata() 但没有任何作用。

想法?

最佳答案

cellForRowAtIndexPath 对于不可见单元格返回 nil

要获得类似单选按钮的效果,您需要执行以下操作:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    configureCell(cell, atIndexPath: indexPath)
    return cell
}

func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
    if indexPath == selectedIndexPath {
        cell.accessoryType = .Checkmark
    } else {
        cell.accessoryType = .None
    }
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedIndexPath = indexPath

    // Note indexPathsForVisibleRows may be nil depending on your app's content.
    // zip just joins two arrays together, into a single array of tuples.
    zip(tableView.indexPathsForVisibleRows!, tableView.visibleCells).forEach({ indexPath, cell in
        configureCell(cell, atIndexPath: indexPath)
    })
}

关于swift - 表格 View 单元格附件在 View 之外时不会更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37246184/

相关文章:

ios - 设置 View Controller 实例的 UIImageView

swift - 如何在 Swift 中表示质量的大小?

xcode - 找不到 Storyboard错误

ios - 尝试将数据传递到另一个 View Controller 时出现 fatal error

ios - 如何获取数组(或某些数据结构?)中的 Assets.xcassets 文件名

arrays - 为什么我不能在 Swift 中使用数组的 append()?

ios - X 位置约束未以编程方式更新 - Xcode 中的 Swift

ios - 固定距离接头无法正常工作

ios - 如何删除特定单元格的模糊 View ?

ios - 单例在 iOS 应用程序上模拟用户?