質問編集履歴
2
メイン画面のソースコード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -66,6 +66,416 @@
|
|
66
66
|
|
67
67
|
|
68
68
|
|
69
|
+
### メイン画面のソースコード
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
```SWIFT
|
74
|
+
|
75
|
+
import UIKit
|
76
|
+
|
77
|
+
import CoreData
|
78
|
+
|
79
|
+
import GrowingTextView
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
class ViewController: UIViewController,UITableViewDelegate,UIGestureRecognizerDelegate {
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
@IBOutlet weak var inputToolbar: UIView!
|
88
|
+
|
89
|
+
@IBOutlet weak var tableView: UITableView!
|
90
|
+
|
91
|
+
@IBOutlet weak var textView: GrowingTextView!
|
92
|
+
|
93
|
+
@IBOutlet weak var textViewBottomConstraint: NSLayoutConstraint!
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
var itemArray = [Item]()
|
98
|
+
|
99
|
+
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
let defaults = UserDefaults.standard
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
override func viewDidLoad() {
|
108
|
+
|
109
|
+
super.viewDidLoad()
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
// *** Customize GrowingTextView ***
|
114
|
+
|
115
|
+
textView.layer.cornerRadius = 4.0
|
116
|
+
|
117
|
+
tableView.contentInset = UIEdgeInsets(top: 15,left: 0,bottom: 15,right: 0)
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
print(FileManager.default.urls(for: .documentDirectory, in: .userDomainMask))
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
textView.delegate = self
|
126
|
+
|
127
|
+
tableView.dataSource = self
|
128
|
+
|
129
|
+
tableView.delegate = self
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
tableView.register(UINib(nibName: "StickyCell", bundle: nil), forCellReuseIdentifier: "ReusableCell")
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
loadItems()
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
// ロングプレス
|
142
|
+
|
143
|
+
let longPressGesture =
|
144
|
+
|
145
|
+
UILongPressGestureRecognizer(target: self,
|
146
|
+
|
147
|
+
action: #selector(ViewController.longPress(_:)))
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
longPressGesture.delegate = self
|
152
|
+
|
153
|
+
self.view.addGestureRecognizer(longPressGesture)
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
override func viewWillAppear(_ animated: Bool) {
|
160
|
+
|
161
|
+
super.viewWillAppear(animated)
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
// Long Press イベント
|
172
|
+
|
173
|
+
@objc func longPress(_ sender: UILongPressGestureRecognizer){
|
174
|
+
|
175
|
+
let point = sender.location(in: tableView)
|
176
|
+
|
177
|
+
let indexPath = tableView.indexPathForRow(at: point)
|
178
|
+
|
179
|
+
if sender.state == .began {
|
180
|
+
|
181
|
+
let alert: UIAlertController = UIAlertController(title: "メッセージの削除", message: "削除してもいいですか?", preferredStyle: UIAlertController.Style.alert)
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
let defaultAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler:{
|
186
|
+
|
187
|
+
(action: UIAlertAction!) -> Void in
|
188
|
+
|
189
|
+
print(indexPath!.row)
|
190
|
+
|
191
|
+
self.context.delete(self.itemArray[indexPath!.row])
|
192
|
+
|
193
|
+
self.itemArray.remove(at: indexPath!.row)
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
self.saveItems()
|
198
|
+
|
199
|
+
})
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
let cancelAction: UIAlertAction = UIAlertAction(title: "キャンセル", style: UIAlertAction.Style.cancel, handler:{
|
204
|
+
|
205
|
+
(action: UIAlertAction!) -> Void in
|
206
|
+
|
207
|
+
print("Cancel")
|
208
|
+
|
209
|
+
})
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
alert.addAction(cancelAction)
|
214
|
+
|
215
|
+
alert.addAction(defaultAction)
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
present(alert, animated: true, completion: nil)
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
@IBAction func addButton(_ sender: UIButton) {
|
228
|
+
|
229
|
+
loadMessages()
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
func textFieldShouldReturn(_ textView: UITextField) -> Bool {
|
236
|
+
|
237
|
+
loadMessages()
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
return false
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
func loadMessages() {
|
250
|
+
|
251
|
+
if textView.text == "" {
|
252
|
+
|
253
|
+
print("error")
|
254
|
+
|
255
|
+
} else {
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
let newItem = Item(context: context)
|
260
|
+
|
261
|
+
newItem.text = textView.text!
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
itemArray.append(newItem)
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
textView.resignFirstResponder()
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
saveItems()
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
DispatchQueue.main.async {
|
278
|
+
|
279
|
+
self.textView.text = ""
|
280
|
+
|
281
|
+
let indexPath = IndexPath(row: self.itemArray.count - 1, section: 0)
|
282
|
+
|
283
|
+
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
func saveItems() {
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
do{
|
296
|
+
|
297
|
+
try context.save()
|
298
|
+
|
299
|
+
} catch {
|
300
|
+
|
301
|
+
print("Error saving context (error)")
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
tableView.reloadData()
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
func loadItems() {
|
316
|
+
|
317
|
+
let request : NSFetchRequest<Item> = Item.fetchRequest()
|
318
|
+
|
319
|
+
do {
|
320
|
+
|
321
|
+
itemArray = try context.fetch(request)
|
322
|
+
|
323
|
+
} catch {
|
324
|
+
|
325
|
+
print("Error fetching data from context (error)")
|
326
|
+
|
327
|
+
}
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
@IBAction func settingButtonAction(_ sender: UIBarButtonItem) {
|
334
|
+
|
335
|
+
performSegue(withIdentifier: "goSetting", sender: nil)
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
func cellUpdate(){
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
itemArray.removeAll()
|
346
|
+
|
347
|
+
print("remove")
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
loadItems()
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
DispatchQueue.main.async {
|
356
|
+
|
357
|
+
self.tableView.reloadData()
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
}
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
extension ViewController: UITableViewDataSource{
|
372
|
+
|
373
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
374
|
+
|
375
|
+
return itemArray.count
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
382
|
+
|
383
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath) as! StickyCell
|
384
|
+
|
385
|
+
cell.label.text = itemArray[indexPath.row].text
|
386
|
+
|
387
|
+
return cell
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
extension UserDefaults {
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
func color(forKey key: String) -> UIColor? {
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
guard let colorData = data(forKey: key) else { return nil }
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
do {
|
408
|
+
|
409
|
+
return try NSKeyedUnarchiver.unarchivedObject(ofClass: UIColor.self, from: colorData)
|
410
|
+
|
411
|
+
} catch let error {
|
412
|
+
|
413
|
+
print("color error (error.localizedDescription)")
|
414
|
+
|
415
|
+
return nil
|
416
|
+
|
417
|
+
}
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
func set(_ value: UIColor?, forKey key: String) {
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
guard let color = value else { return }
|
430
|
+
|
431
|
+
do {
|
432
|
+
|
433
|
+
let data = try NSKeyedArchiver.archivedData(withRootObject: color, requiringSecureCoding: false)
|
434
|
+
|
435
|
+
set(data, forKey: key)
|
436
|
+
|
437
|
+
} catch let error {
|
438
|
+
|
439
|
+
print("error color key data not saved (error.localizedDescription)")
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
}
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
}
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
extension ViewController: GrowingTextViewDelegate {
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
// *** Call layoutIfNeeded on superview for animation when changing height ***
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
func textViewDidChangeHeight(_ textView: GrowingTextView, height: CGFloat) {
|
462
|
+
|
463
|
+
UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: [.curveLinear], animations: { () -> Void in
|
464
|
+
|
465
|
+
self.view.layoutIfNeeded()
|
466
|
+
|
467
|
+
}, completion: nil)
|
468
|
+
|
469
|
+
}
|
470
|
+
|
471
|
+
}
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
```
|
476
|
+
|
477
|
+
|
478
|
+
|
69
479
|
### 設定画面のソースコード(必要部分抜粋)
|
70
480
|
|
71
481
|
|
1
実現したいイメージを追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -169,3 +169,31 @@
|
|
169
169
|
|
170
170
|
|
171
171
|
SWIFT初心者でいたらないこともあると思いますが何卒よろしくお願い申し上げます。
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
### 実現したいイメージ
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
最初の画面
|
180
|
+
|
181
|
+
![イメージ説明](f36f8755fc0d8770bca65eee25055a86.png)
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
設定画面
|
186
|
+
|
187
|
+
![イメージ説明](80a0ce1b4eff504505422a9633e16c6b.png)
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
セルカラーをタップ後カラー変更アラート表示
|
192
|
+
|
193
|
+
![イメージ説明](a866b60c2029c0e9a05d1d5d19dcb1b7.png)
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
カラー変更アラートのカラーを選択でセルの色を変更
|
198
|
+
|
199
|
+
![イメージ説明](08f8ddb0dbd37a6642a1137e05a5b2e9.png)
|