Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCellAccessoryType。选中标记仅在点击另一行时显示选中标记_Ios_Xcode_Uitableview_Swift - Fatal编程技术网

Ios UITableViewCellAccessoryType。选中标记仅在点击另一行时显示选中标记

Ios UITableViewCellAccessoryType。选中标记仅在点击另一行时显示选中标记,ios,xcode,uitableview,swift,Ios,Xcode,Uitableview,Swift,我有以下代码 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell cell.textLabel

我有以下代码

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
}
其目的是在点击行时显示复选标记。有两行,我正在观察以下行为

  • 点击第0行,看不到复选标记
  • 点击第1行,可以看到第0行的复选标记
  • 点击第0行,现在可以看到第1行的复选标记
  • 选择属性设置为(Xcode默认值)

    选择:单选 编辑:编辑期间没有选择


    除了上面的代码外,当点击该行时,我是否应该做些什么来显示复选标记。研究了相关问题,但未能解决。

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

    你需要改变

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


    问题在于XCode建议与UITableView委托方法的类型有关。由于取消选择按字母顺序排在第一位,这是一个非常常见的错误。我曾经用Objective C来面对这个问题,看到Swift也出现了这个问题很有趣。

    DidDecelectRowatindexpath
    更改为
    didSelectRowAtIndexPath
    至少可以说,这个错误具有很强的欺骗性。
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)