【问题标题】:SWIFT: swipe table cell to change it's colorSWIFT:滑动表格单元格以更改其颜色
【发布时间】:2018-07-27 11:06:55
【问题描述】:

我是 Swift(和编码)的新手,所以请多多包涵。在我的桌子上,我设置了单元格,这样当我向右滑动时,我会看到两个选项:“完成”和“删除”。当我单击“删除”时,单元格行被删除。当我点击“完成”时,我希望单元格行改变颜色。我不知道我是怎么走到这一步的,但是当我点击“完成”时,整个表格改变了单元格行的颜色except。我尝试将我的可重用标识符用于单元格原型,我将其设置为“单元格”,但这没有成功,因为我不确定如何声明“单元格”而不干扰我的 CellForRowAt 函数。

这是我的代码:

      func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
        // delete item at indexPath
        self.habits.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
        print(self.habits)
    }
    let done = UITableViewRowAction(style: .default, title: "Done") { (action, indexPath) in
        // save item at indexPath
        tableView.backgroundColor = UIColor.lightGray
        print("Done")
    }

    done.backgroundColor = UIColor.green

    return [delete, done]
}

由于前面的代码块,我认为我不能声明“单元格”:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Dequeue Resuable Cell

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "cell")

    let Habit = habits[indexPath.row]
    cell.textLabel?.text = Habit.mainGoal
    cell.detailTextLabel?.text = Habit.microGoal

    return cell

再次,我是编码新手。我已经尝试查找这个问题,但不太确定我的措辞是否正确,但我找不到答案。我的备用想法是删除单元格中的标签,但从我收集的内容来看,这更复杂。

【问题讨论】:

    标签: swift uitableview row cell


    【解决方案1】:

    你可以这样做

     let done = UITableViewRowAction(style: .default, title: "Done") { (action, indexPath) in
                // save item at indexPath
                let cell = tableView.cellForRow(at: indexPath)
                cell?.backgroundColor = UIColor.green
                print("Done")
            }
    

    【讨论】:

    • 非常感谢!!如果我想让它每天重置,那会是另一回事吗?或者我可以将它包含在这个函数的某个地方吗?
    【解决方案2】:

    在这里您可以找到满足您要求的可能方法。

    https://github.com/SwipeCellKit/SwipeCellKit

    【讨论】:

    • 这不是 OP 问题
    猜你喜欢
    • 2014-08-24
    • 2015-01-24
    • 2019-11-16
    • 2018-12-17
    • 2018-10-04
    • 2016-11-23
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多