UITableViewCellAccessoryType.Checkmark仅在点击另一行时显示复选标记

时间:2015-02-25 07:12:23

标签: ios xcode uitableview swift

我有以下代码

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    cell.textLabel?.text = users[indexPath.row]
    return cell
}

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
    cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}

目的是在点击行时显示复选标记。有两行,我观察到以下行为。

  1. 点按第0行,无法看到复选标记
  2. 点击第1行,可以看到第0行的复选标记
  3. 点击第0行,现在可以看到第1行的复选标记
  4. 选择属性设置为(Xcode默认值)

    选择:单一选择 编辑:编辑期间无选择

    除了上面的代码之外,在点击行时,我还应该做些什么来显示复选标记。看了相关的问题,但无法确定它。

1 个答案:

答案 0 :(得分:2)

这是一个非常常见的问题,我认为几乎所有进行iOS编程的人都面临同样的问题。

您需要更改

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

问题在于XCode建议是一种类型UITableView委托方法。由于取消选择首先按字母顺序排列,因此这是一个非常常见的错误。我曾经在Objective C中面对这个问题,看到Swift出现这个问题也很有趣。

相关问题