【发布时间】:2015-03-17 11:37:36
【问题描述】:
我正在使用 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)
}
}
所以问题确实是双重的。
问题 1
当我按下删除按钮时,我的 commitEditingStyle 似乎没有被调用?我的愚蠢日志消息没有被打印到控制台
问题 2
我无法删除 indexpath 中的对象,因为在 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];
}
这似乎工作正常
【问题讨论】:
-
问题2,如this answer所示,在
self.loadObjects()后面加self.tableView.reloadData()即可。
标签: ios objective-c uitableview swift parse-platform