【问题标题】:Why won't my UITableView's selected cell display the checkmark accessory?为什么我的 UITableView 选择的单元格不显示复选标记附件?
【发布时间】:2016-12-29 09:51:44
【问题描述】:

我正在使用 Swift 3 并试图让一个表格视图单元格突出显示绿色并在点击时显示一个复选标记。

目前,当我运行应用程序时,我可以选择一个单元格,它会变成浅灰色,一旦我选择另一个单元格,它就会变成绿色,但仍然无法显示复选标记。

代码如下:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let mySelectedCell = tableView.cellForRow(at: indexPath)
    mySelectedCell?.accessoryType = UITableViewCellAccessoryType.checkmark
    mySelectedCell?.tintColor = UIColor.white
    mySelectedCell?.backgroundColor = green

}

任何建议将不胜感激。

【问题讨论】:

  • 你在使用自定义的 UITableViewCell 吗?您是否也在验证这个didSelectRowAt 函数是否被调用?
  • 您是否尝试将cell.selectionStyle = .none 添加到cellForRowAtIndexPath

标签: ios swift uitableview swift3


【解决方案1】:

试试:

self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None);

【讨论】:

    【解决方案2】:

    cellForRow 不会获取正在显示的单元格,它会为该索引路径创建一个单元格。您需要保留一组选定的索引,然后为该单元格或 tableView 调用更新,并在 cellForRow 中有添加复选标记附件的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 2011-07-30
      相关资源
      最近更新 更多