我已经检查了其他答案,但他们似乎没有正确回答我的问题。如果有一个我没见过的明显的,请把它放在评论中:)
我所拥有的是一个以Realm数据库作为数据源的tableView。我希望能够从源中删除被删除的单元格。
以下是代码:
func objectsForSection(section: UInt) -> RLMResults {
let categoryName = (Category.allObjects()[section] as Category).name
return Product.objectsWhere("productCategory.name == '\(categoryName)'")
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
var realm = RLMRealm.defaultRealm()
let productsForSection = objectsForSection(UInt(indexPath.section)) // This returns an RLMResults
let product = productsForSection[UInt(indexPath.row)] as Product
realm.beginWriteTransaction()
realm.deleteObject(product)
realm.commitWriteTransaction()
tableView.deleteRowsAtIndexPaths([indexPath.row], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
当我滑动并按删除时崩溃。我做错了什么?
提前致谢!
答案 0 :(得分:2)
你有一句话说:
tableView.deleteRowsAtIndexPaths([indexPath.row], withRowAnimation: .Fade)
传递一个带行号的数组,而不是NSIndexPath
。
我相信你的意思是:
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)