我正在使用Parse和PFQueryTableViewController来显示来自Parse的帖子列表,它正好显示它们。我希望允许用户向左滑动单元格并删除将从Parse中删除它的单元格,并将其从UI中淡出。
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
这可以实现漂亮的iOS8编辑风格。这是问题的起点。当我点击删除按钮时,我可以在控制台中看到我的消息:
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in
println("DELETE•ACTION");
});
return [deleteRowAction];
}
但以下永远不会被调用:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
var object = self.objectAtIndexPath(indexPath)
println("good start")
// The following lines means the project can't build because self.objects is of type NSArray not NSMutableArray
self.objects.removeAtIndex(indexpath)
object.deleteInBackgroundWithBlock({ (Bool, NSError) -> Void in
self.loadObjects()
println("its alive...sort of")
})
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
所以问题确实是双重的。
当我按下删除按钮时,似乎没有调用我的commitEditingStyle?我的愚蠢日志消息没有打印到控制台
我无法删除索引路径中的对象,因为在PFQueryTableViewController中,self.objects是一个NSArray。我不确定如何解决这个问题?我读过一些关于覆盖objectsDidLoad方法并从self.objects创建NSMutableArray或创建子类的内容,但是没有细节,我不明白如何将表的数据源从self.objects更改为self。 myNewMutableArray例如
我已经看到了一些类似的答案,但到目前为止还没有明确的答案。我不介意答案是否在obj C中,我只需要了解我需要做的原则。
根据Rory McKinnel的回答问题解决了。
对于问题2,我创建了以下子类:
class EditablePFQueryTableViewController: PFQueryTableViewController{
var editableObjects: NSMutableArray = NSMutableArray()
override func objectsDidLoad(error: NSError!) {
super.objectsDidLoad(NSError())
editableObjects = NSMutableArray(array: self.objects)
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject! {
var object = self.editableObjects.objectAtIndex(indexPath.row) as PFObject
return object
}
// overriding this to try and make sure the number of rows is accurate
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var numberOfRows = self.editableObjects.count
return numberOfRows
}
}
并补充道:
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in
println("DELETE•ACTION");
var object = self.objectAtIndexPath(indexPath)
self.editableObjects.removeObjectAtIndex(indexPath.row)
object.deleteInBackgroundWithBlock({ (Bool, NSError) -> Void in
self.loadObjects()
println("its alive")
})
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
});
return [deleteRowAction];
}
似乎工作正常
答案 0 :(得分:0)
editActionsForRowAtIndexPath
可让您返回要执行的操作,选中此选项后将触发您注册的处理程序,而不会执行任何操作。您必须将所有删除代码放在处理程序中。您可以在您注册的处理程序中自己调用commitEditingStyle
。这就是您看到NSLog消息的原因。
如果您希望它为您调用commitEditingStyle
,那么您需要实现以下方法,从该方法返回所需的编辑样式以传递indexPath。然后,当用户激活时,它将调用commitEditingStyle
。
optional func tableView(_ tableView: UITableView,
editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle
所以你必须使用其中一种。