ios - 按下按钮时更改行的颜色 - 快速

标签 ios swift uitableview

我有以下代码,当 UITableView 行向左滑动时,会出现两个按钮。

override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
    let more = UITableViewRowAction(style: .normal, title: "Picked") { action, index in
        print("Stock Picked")

    }
    more.backgroundColor = .green

    let favorite = UITableViewRowAction(style: .normal, title: "Not Enough") { action, index in
        print("Not enough stock to pick")
    }
    favorite.backgroundColor = .orange



    return [favorite, more]
}

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

我现在正在尝试这样做,以便如果按下其中一个按钮,它所显示的行就会改变颜色。即,如果向左滑动一行并按下“已选择”按钮,则行颜色将变为绿色。

我还希望它在行上设置某种标志,只有当所有行的标志设置为 true 时,才允许应用程序向前移动。

非常感谢任何建议。

最佳答案

要在编辑操作中更改单元格颜色,您应该使用 UITableViewcellForRowAtIndexpath 方法获取单元格,并将背景颜色更改为您想要的颜色,必须完成此操作UITableViewRowAction

提供的操作 block 内部
override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
    let more = UITableViewRowAction(style: .normal, title: "Picked") { action, index in
        print("Stock Picked")
        let cell = tableView.cellForRow(at: index) as? UITableViewCell
        cell?.backgroundColor = .green
    }
    more.backgroundColor = .green

    let favorite = UITableViewRowAction(style: .normal, title: "Not Enough") { action, index in
        print("Not enough stock to pick")
        let cell = tableView.cellForRow(at: index) as? UITableViewCell
        cell?.backgroundColor = .orange
    }
    favorite.backgroundColor = .orange


    return [favorite, more]
}

关于ios - 按下按钮时更改行的颜色 - 快速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48558597/

相关文章:

objective-c - 如何使用 MobileDevice 框架将文件写入我的应用程序的文档目录?

iOS - 向 Facebook 好友发送请求

swift - 在 Foundation 中找不到 FileManager Swift 类,它在哪里?

swift - 如何让单元测试访问核心数据模型

iphone - UITableViewCell 中的 UITextField

ios - 具有默认样式分组的 UITableView 子类

ios - XCode7、Swift 2、使用解析数据填充 TableView

iOS 从内存流中播放视频

ios - 如何获取 UITableView 单元格选择中哪个元素收到选择?

ios - UITableView UI 不会从新的 NSFetchedResultsController 刷新数据