有没有一种方法可以在用户选择单元格时显示 UITableViewRowAction 按钮(与默认情况下 UITableViewRowAction 的工作方式相反,默认情况下需要 tableview 处于编辑模式)。
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{
let shareAction = UITableViewRowAction(style: .Normal, title: "Share") { (rowAction, indexPath) in
print("Share Button tapped. Row item value = \(self.itemsToLoad[indexPath.row])")
self.displayShareSheet(indexPath)
}
let deleteAction = UITableViewRowAction(style: .Default, title: "Delete") { (rowAction, indexPath) in
print("Delete Button tapped. Row item value = \(self.itemsToLoad[indexPath.row])")
}
shareAction.backgroundColor = UIColor.greenColor()
return [shareAction,deleteAction]
}
最佳答案
您不太可能会在另一个答案中找到可以放入您的项目并让它运行的代码,尤其是如果该代码是用另一种语言编写的。您将修改您的代码来执行您想要的操作。
一般来说,您不应该直接操作您的细胞。您应该设置数据模型以包含用于配置单元格的状态信息。您可能有一个结构数组,其中每个数组条目描述要在单个单元格中显示的信息。 (如果您使用分段 TableView ,您可能会使用一个数组数组,其中外部数组是您的部分,每个内部数组包含一个部分中行的结构。)
下面概述了您可以如何实现它:
struct CellData {
var title: String
var showButtons: Bool
}
然后在您的tabView(_:didSelect:)
方法中,将先前选择的indexPath(如果有)上的showButtons
标志设置为false,将将新选择的单元格上的 showButtons
标记为 true
,并调用 reloadRows(at:with:)
重新绘制取消选择的单元格和新选择的单元格。
如果您允许一次选择多个单元格,那么您将不会清除先前选择的单元格的 showButtons
标志。
在您的 cellForRow(at:)
方法中,使用 showButtons 标志来决定您的按钮是否应该被隐藏。
关于swift - 使用swift 4在表格 View 中选择行时显示两个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48770690/