質問編集履歴
7
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
6
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
-
|
3
2
|
CollectionViewとInputViewという2つの画面をもつアプリで、
|
4
|
-
|
3
|
+
InputViewで名前や写真を入力して完了ボタンを押すと、collectionViewで登録された情報が一覧で見れるようにしたいです。
|
5
4
|
|
6
5
|
### 発生している問題・エラーメッセージ
|
7
6
|
|
5
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
### 前提・実現したいこと
|
2
2
|
|
3
3
|
CollectionViewとInputViewという2つの画面をもつアプリで、
|
4
4
|
・InputViewで名前や写真を入力して完了ボタンを押すと、collectionViewで登録された情報が一覧で見れるようにしたいです。
|
4
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
### 前提・実現したいこと
|
1
|
+
> 引用テキスト### 前提・実現したいこと
|
2
2
|
|
3
3
|
CollectionViewとInputViewという2つの画面をもつアプリで、
|
4
4
|
・InputViewで名前や写真を入力して完了ボタンを押すと、collectionViewで登録された情報が一覧で見れるようにしたいです。
|
@@ -8,7 +8,7 @@
|
|
8
8
|
完了ボタンを押してCollectionViewに戻ると、データ数は増えているが、全て空の状態になっている
|
9
9
|
|
10
10
|
### 該当のソースコード
|
11
|
-
```Teammate
|
11
|
+
```Teammate
|
12
12
|
import Foundation
|
13
13
|
import RealmSwift
|
14
14
|
|
3
誤字の修正をしました
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
完了ボタンを押してCollectionViewに戻ると、データ数は増えているが、全て空の状態になっている
|
9
9
|
|
10
10
|
### 該当のソースコード
|
11
|
-
```Teammate
|
11
|
+
```Teammate Model
|
12
12
|
import Foundation
|
13
13
|
import RealmSwift
|
14
14
|
|
@@ -86,7 +86,7 @@
|
|
86
86
|
nameText.text = nm
|
87
87
|
}
|
88
88
|
```
|
89
|
-
```InfoViewController
|
89
|
+
```InfoViewController
|
90
90
|
import UIKit
|
91
91
|
|
92
92
|
class InfoViewController: UIViewController {
|
@@ -106,7 +106,7 @@
|
|
106
106
|
parent.name = nameTextField.text!
|
107
107
|
}
|
108
108
|
```
|
109
|
-
```ViewController
|
109
|
+
```ViewController
|
110
110
|
import UIKit
|
111
111
|
import Charts
|
112
112
|
import RealmSwift
|
2
空欄のまましつもんしてしまったので、情報を入れました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,25 +1,195 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
ここに質問の内容を詳しく書いてください。
|
4
|
-
|
3
|
+
CollectionViewとInputViewという2つの画面をもつアプリで、
|
5
|
-
|
4
|
+
・InputViewで名前や写真を入力して完了ボタンを押すと、collectionViewで登録された情報が一覧で見れるようにしたいです。
|
6
5
|
|
7
6
|
### 発生している問題・エラーメッセージ
|
8
7
|
|
8
|
+
完了ボタンを押してCollectionViewに戻ると、データ数は増えているが、全て空の状態になっている
|
9
|
+
|
10
|
+
### 該当のソースコード
|
11
|
+
```Teammate(Model)
|
12
|
+
import Foundation
|
13
|
+
import RealmSwift
|
14
|
+
|
15
|
+
class Teammate: Object {
|
16
|
+
// ID
|
17
|
+
dynamic var id = 0
|
18
|
+
// 名前
|
19
|
+
dynamic var name = ""
|
20
|
+
// 写真
|
21
|
+
dynamic var photo = Data()
|
22
|
+
}
|
9
23
|
```
|
24
|
+
```InputViewController
|
10
|
-
|
25
|
+
import UIKit
|
26
|
+
import RealmSwift
|
27
|
+
|
28
|
+
class InputViewController: UIViewController, UIImagePickerControllerDelegate & UINavigationControllerDelegate {
|
29
|
+
|
30
|
+
@IBOutlet weak var imageView: UIImageView!
|
31
|
+
@IBOutlet weak var infoView: UIView!
|
32
|
+
@IBOutlet weak var nameText: UILabel!
|
33
|
+
|
34
|
+
var name = ""
|
35
|
+
var imagePhoto: UIImage!
|
36
|
+
|
37
|
+
override func viewDidLoad() {
|
38
|
+
super.viewDidLoad()
|
39
|
+
// Do any additional setup after loading the view.
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
@IBAction func tapAction(_ sender: Any) {
|
44
|
+
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
|
45
|
+
// 写真を選ぶビュー
|
46
|
+
let pickerView = UIImagePickerController()
|
47
|
+
// 写真の選択元をカメラロールにする
|
48
|
+
pickerView.sourceType = .photoLibrary
|
49
|
+
// デリゲート
|
50
|
+
pickerView.delegate = self
|
51
|
+
//編集を可能にする
|
52
|
+
pickerView.allowsEditing = true
|
53
|
+
// ビューに表示
|
54
|
+
self.present(pickerView, animated: true)
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
|
59
|
+
if info[UIImagePickerController.InfoKey.originalImage] != nil {
|
60
|
+
// 選択した写真を取得する
|
61
|
+
let image = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
|
62
|
+
// ビューに表示する
|
63
|
+
imageView.image = image
|
64
|
+
imagePhoto = image
|
65
|
+
// 写真を選ぶビューを引っ込める
|
66
|
+
self.dismiss(animated: true)
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
//登録ボタン
|
71
|
+
@IBAction func okayBtnAction(_ sender: Any) {
|
72
|
+
//データの更新
|
73
|
+
let realm = try! Realm()
|
74
|
+
let newData = Teammate()
|
75
|
+
try! realm.write {
|
76
|
+
newData.name = nameText.text!
|
77
|
+
newData.photo = imagePhoto.jpegData(compressionQuality: 0.5)! as Data
|
78
|
+
realm.add(newData)
|
79
|
+
print(newData.name)
|
80
|
+
print(newData.photo)
|
81
|
+
}
|
82
|
+
self.dismiss(animated: true, completion: nil)
|
83
|
+
}
|
84
|
+
|
85
|
+
@objc func changeName(nm: String){
|
86
|
+
nameText.text = nm
|
87
|
+
}
|
11
88
|
```
|
89
|
+
```InfoViewController(InputViewの子View)
|
90
|
+
import UIKit
|
12
91
|
|
13
|
-
|
92
|
+
class InfoViewController: UIViewController {
|
14
93
|
|
94
|
+
@IBOutlet weak var nameTextField: UITextField!
|
95
|
+
|
96
|
+
override func viewDidLoad() {
|
15
|
-
|
97
|
+
super.viewDidLoad()
|
98
|
+
nameTextField.addTarget(self, action: #selector(changeText), for: .editingChanged)
|
99
|
+
|
100
|
+
// Do any additional setup after loading the view.
|
101
|
+
}
|
102
|
+
|
16
|
-
|
103
|
+
@objc func changeText(){
|
104
|
+
let parent = self.parent as! InputViewController
|
105
|
+
parent.changeName(nm: nameTextField.text!)
|
106
|
+
parent.name = nameTextField.text!
|
107
|
+
}
|
17
108
|
```
|
109
|
+
```ViewController(CollectionViewと紐づく)
|
110
|
+
import UIKit
|
111
|
+
import Charts
|
112
|
+
import RealmSwift
|
18
113
|
|
114
|
+
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
|
115
|
+
|
116
|
+
@IBOutlet weak var collectionView: UICollectionView!
|
117
|
+
|
118
|
+
//Realmインスタンスを取得
|
119
|
+
let realm = try! Realm()
|
120
|
+
|
121
|
+
//Teammateが格納されているリスト
|
122
|
+
var teammate = try! Realm().objects(Teammate.self)
|
123
|
+
|
124
|
+
override func viewDidLoad() {
|
125
|
+
super.viewDidLoad()
|
126
|
+
// Do any additional setup after loading the view.
|
127
|
+
collectionView.dataSource = self
|
128
|
+
|
129
|
+
for human in teammate {
|
130
|
+
print("name: (human.name)")
|
131
|
+
}
|
132
|
+
|
133
|
+
// レイアウトを調整
|
134
|
+
let layout = UICollectionViewFlowLayout()
|
135
|
+
|
136
|
+
layout.sectionInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
|
137
|
+
collectionView.collectionViewLayout = layout
|
138
|
+
}
|
139
|
+
|
140
|
+
//セル表示数
|
141
|
+
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
142
|
+
// 表示するセルの数
|
143
|
+
return teammate.count
|
144
|
+
}
|
145
|
+
|
146
|
+
//各セルの内容を返すメソッド
|
147
|
+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
148
|
+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
|
149
|
+
let person = teammate[indexPath.row]
|
150
|
+
//写真のデータ型の変換
|
151
|
+
var imageData: UIImage? = UIImage(data: person.photo as Data)
|
152
|
+
if imageData == nil {
|
153
|
+
imageData = UIImage(named: "sample")
|
154
|
+
}
|
155
|
+
//Cellにセット
|
156
|
+
cell.setup(name: person.name, icon: imageData!)
|
157
|
+
// セルの色
|
158
|
+
//cell.backgroundColor = .lightGray
|
159
|
+
return cell
|
160
|
+
}
|
161
|
+
|
162
|
+
//デザイン
|
163
|
+
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
164
|
+
let horizontalSpace : CGFloat = 20
|
165
|
+
let cellSize : CGFloat = self.view.bounds.width / 3 - horizontalSpace
|
166
|
+
return CGSize(width: cellSize, height: cellSize)
|
167
|
+
}
|
168
|
+
|
169
|
+
//入力画面から返ってきたときにCollectionViewを更新
|
170
|
+
override func viewWillAppear(_ animated: Bool) {
|
171
|
+
super.viewWillAppear(animated)
|
172
|
+
collectionView.reloadData()
|
173
|
+
}
|
174
|
+
}
|
175
|
+
```
|
176
|
+
```CollectionViewCell
|
177
|
+
import UIKit
|
178
|
+
|
179
|
+
class CollectionViewCell: UICollectionViewCell {
|
180
|
+
@IBOutlet weak var cellName: UILabel!
|
181
|
+
@IBOutlet weak var cellImage: UIImageView!
|
182
|
+
|
183
|
+
func setup(name: String, icon: UIImage){
|
184
|
+
cellName.text = name
|
185
|
+
cellImage.image = icon
|
186
|
+
}
|
187
|
+
}
|
188
|
+
```
|
19
189
|
### 試したこと
|
190
|
+
Realm Studioを使ってデータを確認しましたが、全て空でした
|
20
191
|
|
21
|
-
ここに問題に対して試したことを記載してください。
|
22
|
-
|
23
192
|
### 補足情報(FW/ツールのバージョンなど)
|
24
|
-
|
193
|
+
iOS 14.3
|
194
|
+
Xcode 12.3
|
25
|
-
|
195
|
+
RealmSwift 10.5.0
|
1
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|