如何在Swift 5中为表格视图单元格添加滑动操作

t40tm48m  于 5个月前  发布在  Swift
关注(0)|答案(2)|浏览(53)

我想添加编辑操作,以便在用户滑动表行时显示。我以前可以使用tableView(_:editActionsForRowAt:)方法,但现在已弃用。在tableView(_:commit:forRowAt:)方法中,没有我需要的操作。如何为单元格添加操作?

4ktjp1zp

4ktjp1zp1#

尝试以下方法:

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let item = UIContextualAction(style: .destructive, title: "Delete") {  (contextualAction, view, boolValue) in
            //Write your code in here
        }
        item.image = UIImage(named: "deleteIcon")

        let swipeActions = UISwipeActionsConfiguration(actions: [item])
    
        return swipeActions
    }

字符串

ttisahbt

ttisahbt2#

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
        // delete item at indexPath
    }

    let share = UITableViewRowAction(style: .normal, title: "Disable") { (action, indexPath) in
        // share item at indexPath
    }

    share.backgroundColor = UIColor.blue

    return [delete, share]
}

字符串
请注意,第一个按钮使用.destructive样式,因此默认情况下它将被着色为红色,但第二个按钮被指定为蓝色。

相关问题