質問編集履歴
1
全体コードを追記致しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -39,4 +39,73 @@
|
|
39
39
|
Xcode Version 14.0.1 (14A400)
|
40
40
|
|
41
41
|
ここにより詳細な情報を記載してください。
|
42
|
+
```全体のコード
|
43
|
+
import UIKit
|
44
|
+
import RealmSwift
|
45
|
+
class ItemTableViewController: UITableViewController {
|
46
|
+
let realm = try! Realm()
|
47
|
+
let item = TodoItem()
|
48
|
+
var itemList: Results<TodoItem>!
|
49
|
+
var token: NotificationToken!
|
42
50
|
|
51
|
+
override func viewDidLoad() {
|
52
|
+
super.viewDidLoad()
|
53
|
+
tableView.register(UINib(nibName: "ItemTableViewCell", bundle: nil), forCellReuseIdentifier: "checkListCell")
|
54
|
+
|
55
|
+
itemList = realm.objects(TodoItem.self).sorted(byKeyPath: "name")
|
56
|
+
token = realm.observe{notification,realm in
|
57
|
+
self.tableView.reloadData()
|
58
|
+
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
63
|
+
return itemList.count
|
64
|
+
}
|
65
|
+
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
|
66
|
+
if editingStyle == .delete {
|
67
|
+
try! realm.write {
|
68
|
+
tableView.deleteRows(at: [indexPath], with: .fade)
|
69
|
+
realm.delete(itemList[indexPath.row])
|
70
|
+
tableView.reloadData()
|
71
|
+
}
|
72
|
+
tableView.reloadData()
|
73
|
+
}
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
79
|
+
return 80
|
80
|
+
}
|
81
|
+
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
82
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "checkListCell", for: indexPath) as! ItemTableViewCell
|
83
|
+
let item = itemList[indexPath.row]
|
84
|
+
if item.done == false {
|
85
|
+
cell.checkImageView?.image = UIImage(named: "Image")
|
86
|
+
}else{
|
87
|
+
cell.checkImageView.image = UIImage(named: "check")
|
88
|
+
}
|
89
|
+
cell.nameLabel.text = item.name
|
90
|
+
|
91
|
+
|
92
|
+
return cell
|
93
|
+
}
|
94
|
+
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
|
95
|
+
if let item = itemList?[indexPath.row] {
|
96
|
+
|
97
|
+
do {
|
98
|
+
try! realm.write {
|
99
|
+
|
100
|
+
item.done = !item.done
|
101
|
+
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
tableView.reloadData()
|
106
|
+
}
|
107
|
+
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
```
|