我正在构建一个非常基本的笔记记录应用程序,我想要的其中一个功能是尝试添加幻灯片以删除/共享功能。
这是我目前的代表实施:
extension SomeViewController: UITableViewDelegate {
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("Selected indexpath: \(indexPath)")
}
func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
return true
}
// Why does this have to be here? It doesn't do anything, it never gets called, and yet without it, the project won't run
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
println("Commit Editing Style \(editingStyle)")
}
func tableView(tableView: UITableView!, editActionsForRowAtIndexPath indexPath: NSIndexPath!) -> [AnyObject]! {
var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Share", handler: {
(action: UITableViewRowAction!, indexPath: NSIndexPath!) in
println("Triggered share action \(action) atIndexPath: \(indexPath)")
return
})
var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler: {
(action: UITableViewRowAction!, indexPath: NSIndexPath!) in
println("Triggered delete action \(action) atIndexPath: \(indexPath)")
return
})
return [deleteAction, shareAction]
}
}
注意 我将返回值添加到处理程序闭包中,因为在Swift中,单行闭包隐式地将该值作为参数返回,而Beta 4则抛出错误。我只想展示这个概念,所以我不需要多行...
问题
上面的函数commitEditingStyle
永远不会被调用,我也不会做任何事情。但是,如果我将其删除,该应用程序将不再起作用。
包含commitEditingStyle
未包含commitEditingStyle
注意
在撰写此问题时,我正在运行Xcode Beta 4及其当前的模拟器实现。
修改
在Objective-C中还有一个关于此问题的主题here.如果你阅读了这些评论,你会发现存在同样的问题。
答案 0 :(得分:0)
它可能是Apple关于如何实现UITableViews的实现细节,但假设如果要显示这样的按钮,你需要一种方法来处理他们的行为,这是什么commitEditingStyle
是为了。根据您的注意事项,他们可能会检查您是否已实现该选择器(respondsToSelector
),如果是,则会创建视图和动画以允许幻灯片显示更多操作。这样,如果用户点击按钮,则不会发送到实例""无法识别的选择器。抛出。
请记住,选择器commitEditingStyle
在协议定义中是@optional,因此您不必实现它。但是,UITableView知道如果你没有它,为什么它会在代码中显示可能导致异常的任何内容。