Пытаясь создать действительно простое приложение, и я полностью застрял с редактированием существующих элементов в списке.
Это файл Item.swift:
import Foundation
import RealmSwift
class Item: Object {
@objc dynamic var title: String = ""
@objc dynamic var done: Bool = false
@objc dynamic var dateCreated: Date?
@objc dynamic var order: Int = 0
var parentCategory = LinkingObjects(fromType: Category.self, property: "items")
}
Идея состояла в том, чтобы провести пальцем влево, чтобы получить правку и кнопку удаления:
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath)
-> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete",
handler: handleRowAction)
let edit = UITableViewRowAction(style: .normal, title: "Edit",
handler: handleRowAction)
edit.backgroundColor = UIColor.blue
return [delete, edit]
}
func handleRowAction(action: UITableViewRowAction, indexPath: IndexPath){
print("Action is \(action.title!) at Index Path \(indexPath)")
let actionItem = action.title
switch actionItem {
case "Edit":
print("Edit function")
self.editModel(at: indexPath)
case "Delete":
print("Delete function")
self.updateModel(at: indexPath)
default:
print("Default just in case")
}
}
func updateModel(at indexPath: IndexPath) {
// Update our data model
}
func editModel(at indexPath: IndexPath) {
//Update our data model
}
У меня не было проблем с удалением элементов, но я не могу понять, как сделать функцию редактирования. В настоящее время, когда я нажимаю кнопку редактирования, всплывающее текстовое поле показывает текущий элемент, но когда я набираю обновление и нажимаю на кнопку, ничего не происходит, очевидно.
override func editModel(at indexPath: IndexPath) {
var textField = UITextField()
let alert = UIAlertController(title: "Edit Item", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Edit Item", style: .default) { (action) in
var updatedItem = self.todoItems?[indexPath.row].title
updatedItem = textField.text!
if self.selectedCategory?.items != nil {
do {
try self.realm?.write {
// realm?.add(updatedItem, update: true)
}
} catch {
print("Error savig new items, \(error)")
}
}
self.tableView.reloadData()
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = self.todoItems?[indexPath.row].title
textField = alertTextField
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
Как вы можете видеть Я попытался
realm?.add(updatedItem, update: true)
, но выдает следующее сообщение об ошибке: Не удается преобразовать значение типа 'String?' к ожидаемому типу аргумента 'Object'.
Как я могу это исправить? Спасибо!