質問編集履歴
1
コードを変更します
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
今、Realmで日記アプリを作成していて投稿や更新の仕方はわかったのですが、削除の仕方がわからず、困っています。
|
1
|
+
今、Realmで日記アプリを作成していて投稿や更新の仕方はわかったのですが、削除の仕方がわからず、困っています。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
|
5
|
+
自己解決しましたので下に書いておきます。
|
6
6
|
|
7
7
|
|
8
8
|
|
@@ -10,142 +10,36 @@
|
|
10
10
|
|
11
11
|
import UIKit
|
12
12
|
|
13
|
-
import FSCalendar
|
14
|
-
|
15
|
-
import CalculateCalendarLogic
|
16
|
-
|
17
13
|
import Realm
|
18
14
|
|
19
15
|
import RealmSwift
|
20
16
|
|
21
17
|
|
22
18
|
|
23
|
-
class CalendarViewController:
|
19
|
+
class CalendarViewController: Object {
|
24
20
|
|
25
|
-
|
21
|
+
func delete() { //削除する
|
26
22
|
|
27
|
-
|
23
|
+
try! Diary.realm.write {
|
28
24
|
|
29
|
-
|
30
|
-
|
31
|
-
@IBOutlet weak var Calendarview: FSCalendar! //関連付けしてあげます
|
32
|
-
|
33
|
-
@IBOutlet weak var DiaryTableView: UITableView!
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
override func viewDidLoad() {
|
38
|
-
|
39
|
-
super.viewDidLoad()
|
40
|
-
|
41
|
-
// Do any additional setup after loading the view.
|
42
|
-
|
43
|
-
Calendarview.dataSource = self //初期化していきます
|
44
|
-
|
45
|
-
Calendarview.delegate = self //初期化していきます
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
25
|
+
Diary.realm.delete(self)
|
50
|
-
|
51
|
-
configureTableView()
|
52
26
|
|
53
27
|
}
|
54
|
-
|
55
|
-
func configureTableView() {
|
56
|
-
|
57
|
-
//dataSourceとdelegateメソッドが使えるようにしました
|
58
|
-
|
59
|
-
DiaryTableView.delegate = self //初期化していきます
|
60
|
-
|
61
|
-
DiaryTableView.dataSource = self //初期化していきます
|
62
|
-
|
63
|
-
DiaryTableView.tableFooterView = UIView() //余白を消す
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
override func viewWillAppear(_ animated: Bool) {
|
70
|
-
|
71
|
-
DiaryArray = Diary.search(date: Date())
|
72
|
-
|
73
|
-
DiaryTableView.reloadData()
|
74
|
-
|
75
|
-
}
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
override func didReceiveMemoryWarning() {
|
80
|
-
|
81
|
-
super.didReceiveMemoryWarning()
|
82
|
-
|
83
|
-
// Dispose of any resources that can be recreated.
|
84
|
-
|
85
|
-
}
|
86
|
-
|
87
|
-
```
|
88
|
-
|
89
|
-
```swift
|
90
|
-
|
91
|
-
extension CalendarViewController: UITableViewDelegate, UITableViewDataSource {
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
96
|
-
|
97
|
-
return DiaryArray.count
|
98
|
-
|
99
|
-
}
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
104
|
-
|
105
|
-
let cell = tableView.dequeueReusableCell(withIdentifier: "DiaryCell")!
|
106
|
-
|
107
|
-
cell.textLabel?.text = DiaryArray[indexPath.row].title
|
108
|
-
|
109
|
-
return cell
|
110
|
-
|
111
|
-
}
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
116
|
-
|
117
|
-
self.performSegue(withIdentifier: "toDetails", sender: nil) // 詳細画面に遷移します
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
|
126
|
-
|
127
|
-
return .delete
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
|
134
|
-
|
135
|
-
if editingStyle == .delete{
|
136
|
-
|
137
|
-
try! realm.write {
|
138
|
-
|
139
|
-
tableView.deleteRows(at: [indexPath], with: .fade)
|
140
|
-
|
141
|
-
realm.delete(taskArray[indexPath.row])
|
142
|
-
|
143
|
-
}
|
144
|
-
|
145
|
-
}
|
146
28
|
|
147
29
|
}
|
148
30
|
|
149
31
|
}
|
150
32
|
|
151
33
|
```
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
```swift
|
38
|
+
|
39
|
+
@IBAction override func delete(_ sender: Any?) {
|
40
|
+
|
41
|
+
selectedDiary.delete()
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
```
|