質問編集履歴
1
ソース提示
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,3 +15,241 @@
|
|
15
15
|
質問があればお願いします
|
16
16
|
|
17
17
|
簡単ですがよろしくお願いします
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
参考にしたサイトはブックマーク等していなかったためわかりません。
|
22
|
+
|
23
|
+
すみません
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```swift
|
28
|
+
|
29
|
+
import UIKit
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
@IBOutlet var tableView: UITableView!
|
38
|
+
|
39
|
+
var todoarray = [String]()
|
40
|
+
|
41
|
+
var todoarraydate = [String]()
|
42
|
+
|
43
|
+
var todocontent = [String]()
|
44
|
+
|
45
|
+
var refresher: UIRefreshControl?
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
override func viewDidLoad() {
|
50
|
+
|
51
|
+
super.viewDidLoad()
|
52
|
+
|
53
|
+
if UserDefaults.standard.array(forKey: "todoarray") == nil {
|
54
|
+
|
55
|
+
todoarray = [String]()
|
56
|
+
|
57
|
+
UserDefaults.standard.set(todoarray, forKey: "todoarray")
|
58
|
+
|
59
|
+
} else {
|
60
|
+
|
61
|
+
todoarray = UserDefaults.standard.array(forKey: "todoarray") as! [String]
|
62
|
+
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
if UserDefaults.standard.array(forKey: "todoarraydate") == nil {
|
68
|
+
|
69
|
+
todoarraydate = [String]()
|
70
|
+
|
71
|
+
UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate")
|
72
|
+
|
73
|
+
} else {
|
74
|
+
|
75
|
+
todoarraydate = UserDefaults.standard.array(forKey: "todoarraydate") as! [String]
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
if UserDefaults.standard.array(forKey: "todocontent") == nil {
|
82
|
+
|
83
|
+
todocontent = [String]()
|
84
|
+
|
85
|
+
UserDefaults.standard.set(todocontent, forKey: "todocontent")
|
86
|
+
|
87
|
+
} else {
|
88
|
+
|
89
|
+
todocontent = UserDefaults.standard.array(forKey: "todocontent") as! [String]
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
refresher = UIRefreshControl()
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
refresher?.attributedTitle = NSAttributedString(string: "Pull to refresh")
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
refresher?.addTarget(self, action: #selector(FirstViewController.refresh), for: UIControlEvents.valueChanged)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
self.tableView.addSubview(refresher!)
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
refresh()
|
112
|
+
|
113
|
+
// Do any additional setup after loading the view, typically from a nib.
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
override func didReceiveMemoryWarning() {
|
120
|
+
|
121
|
+
super.didReceiveMemoryWarning()
|
122
|
+
|
123
|
+
// Dispose of any resources that can be recreated.
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
internal func numberOfSections(in tableView: UITableView) -> Int {
|
130
|
+
|
131
|
+
return 1
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
func refresh() {
|
138
|
+
|
139
|
+
tableView.reloadData()
|
140
|
+
|
141
|
+
refresher?.endRefreshing()
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
148
|
+
|
149
|
+
if todoarray.count == todoarraydate.count {
|
150
|
+
|
151
|
+
return todoarray.count
|
152
|
+
|
153
|
+
} else {
|
154
|
+
|
155
|
+
return todoarraydate.count
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
164
|
+
|
165
|
+
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
|
166
|
+
|
167
|
+
if cell == nil {
|
168
|
+
|
169
|
+
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
cell?.textLabel?.text = todoarray[indexPath.row]
|
174
|
+
|
175
|
+
cell?.detailTextLabel?.text = String(describing: todoarraydate[indexPath.row])
|
176
|
+
|
177
|
+
return cell!
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
internal func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
184
|
+
|
185
|
+
// Return false if you do not want the specified item to be editable.
|
186
|
+
|
187
|
+
return true
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
@IBAction func new(_ sender: AnyObject) {
|
194
|
+
|
195
|
+
self.performSegue(withIdentifier: "New To Do", sender: self)
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
|
202
|
+
|
203
|
+
if editingStyle == .delete {
|
204
|
+
|
205
|
+
todoarray.remove(at: indexPath.row)
|
206
|
+
|
207
|
+
todoarraydate.remove(at: indexPath.row)
|
208
|
+
|
209
|
+
todocontent.remove(at: indexPath.row)
|
210
|
+
|
211
|
+
tableView.deleteRows(at: [indexPath], with: .fade)
|
212
|
+
|
213
|
+
UserDefaults.standard.set(todoarray, forKey: "todoarray")
|
214
|
+
|
215
|
+
UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate")
|
216
|
+
|
217
|
+
UserDefaults.standard.set(todocontent, forKey: "todocontent")
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
override func prepare(for segue: UIStoryboardSegue,sender: Any?){
|
226
|
+
|
227
|
+
//if segue.identifier == "toSubViewController" {
|
228
|
+
|
229
|
+
//let SubVC: SubViewController = segue.destination as! SubViewController
|
230
|
+
|
231
|
+
//let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow! as NSIndexPath
|
232
|
+
|
233
|
+
//SubVC.a = array[indexPath]
|
234
|
+
|
235
|
+
//}
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
//画面遷移
|
242
|
+
|
243
|
+
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
244
|
+
|
245
|
+
print("\(indexPath)")
|
246
|
+
|
247
|
+
performSegue(withIdentifier: "toSubViewController", sender: nil)
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
```
|