我有一个填充了Realm数组的tableView,列表数组是从其他3个Realm结果数组中组合而来的,如下面的代码所示。现在我想为每个单元格实现删除操作。但该应用程序返回了这些例外,所以请任何人都可以帮我解决这个问题吗?非常感谢你们。
class BookmarksVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
var articleList: Results<DynamicObject>!
var documentList: Results<DynamicObject>!
var itArticleList: Results<DynamicObject>!
var list = [Any]()
var article: Any!
var searchBar:UISearchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: 320, height: 20))
func loadDataAndUpdateUI() {
articleList = realm.dynamicObjects(NewsArticle.className())
documentList = realm.dynamicObjects(Documents.className())
itArticleList = realm.dynamicObjects(ITNewsArticle.className())
for article in articleList {
list.append(article)
}
for document in documentList {
list.append(document)
}
for itArticle in itArticleList{
list.append(itArticle)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
print(list.count)
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
article = list[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "bookmarkCell", for: indexPath) as! BookmarkCell
cell.configureCell(article:article)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .default, title: "Delete") { (deleteAction, indexPath) -> Void in
//Deletion will go here
let itemToBeDeleted = self.list[indexPath.row]
try! realm.write {
realm.delete(itemToBeDeleted as! Object)
}
}
return [deleteAction]
}
我还会附加用于创建这些对象的代码:
func saveFavoriteArticle() {
try! realm.write {
realm.add(bookmarkItem!)
}
print(realm.dynamicObjects(NewsArticle.className()))
}
//Same code for 2 remaining objects