【问题标题】:can't disable cell based on background color swift无法根据背景颜色快速禁用单元格
【发布时间】:2024-01-10 20:17:01
【问题描述】:

我正在制作一个应用程序,它使用不同颜色的单元格将单元格分成不同的类别,但我还有一个功能,允许用户点击单元格以添加复选标记并选择它们。我希望禁用彩色单元格,以便当用户点击它们时,他们不会向单元格添加复选标记并选择它。 这是我的复选标记功能:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cellIdentifier = "instrumentTableCell"
    let cell: InstrumentTableCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? InstrumentTableCell

    checked = Array(count:recipies.count, repeatedValue:false)
    cell.configurateTheCell(recipies[indexPath.row])

    if !checked[indexPath.row] {
        cell.accessoryType = .None
    } else if checked[indexPath.row] {
        cell.accessoryType = .Checkmark
    }


    return cell
}

【问题讨论】:

  • cell.userInteractionEnabled 有帮助吗?
  • @paulvs 是的,谢谢!
  • 我将其添加为答案。
  • 如果您对某个答案感到满意,您可以通过单击帖子左侧的复选标记将其标记为已接受的答案。

标签: swift uitableview swift2 cell


【解决方案1】:

试试cell.userInteractionEnabled = false

【讨论】:

    【解决方案2】:

    使用 UITableViewDelegate 函数

    tableView:willDisplayCell:forRowAtIndexPath:
    

    您可以在那里访问单元格功能,例如字体和背景颜色:

    cell.backgroundColor
    cell.textLabel.backgroundColor
    cell.detailTextLabel.backgroundColor
    

    然后,您可以根据颜色启用/禁用单元格以使用

    进行点击
    cell.userInteractionEnabled = false
    

    如有必要,您也可以在此处启用/禁用单元格的其他部分。

    【讨论】: