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
}
其目的是在点击行时显示复选标记。有两行,我正在观察以下行为
除了上面的代码外,当点击该行时,我是否应该做些什么来显示复选标记。研究了相关问题,但未能解决。这是一个非常常见的问题,我认为几乎所有做iOS编程的人都面临着同样的问题 你需要改变
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
到
问题在于XCode建议与UITableView委托方法的类型有关。由于取消选择按字母顺序排在第一位,这是一个非常常见的错误。我曾经用Objective C来面对这个问题,看到Swift也出现了这个问题很有趣。将
DidDecelectRowatindexpath
更改为didSelectRowAtIndexPath
至少可以说,这个错误具有很强的欺骗性。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)