質問編集履歴
2
HomeViewControllerを表示した
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,11 +14,15 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
-
### ソースコード
|
17
|
+
### ソースコード(データクラス)
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
-
```
|
21
|
+
```
|
22
|
+
|
23
|
+
import Foundation
|
24
|
+
|
25
|
+
|
22
26
|
|
23
27
|
class User:NSObject, NSCoding {
|
24
28
|
|
@@ -32,6 +36,18 @@
|
|
32
36
|
|
33
37
|
var UserImage: NSData?
|
34
38
|
|
39
|
+
var lives: String?
|
40
|
+
|
41
|
+
var sex: String?
|
42
|
+
|
43
|
+
var age: String?
|
44
|
+
|
45
|
+
var colleage: String?
|
46
|
+
|
47
|
+
var event: String?
|
48
|
+
|
49
|
+
var eventName: String?
|
50
|
+
|
35
51
|
var like = [String]()
|
36
52
|
|
37
53
|
|
@@ -54,7 +70,19 @@
|
|
54
70
|
|
55
71
|
UserImage = aDecoder.decodeObject(forKey: "userimage") as? NSData
|
56
72
|
|
73
|
+
lives = aDecoder.decodeObject(forKey: "lives") as? String
|
74
|
+
|
75
|
+
sex = aDecoder.decodeObject(forKey: "sex") as? String
|
76
|
+
|
77
|
+
age = aDecoder.decodeObject(forKey: "age") as? String
|
78
|
+
|
79
|
+
colleage = aDecoder.decodeObject(forKey: "colleage") as? String
|
80
|
+
|
57
|
-
like = aDecoder.decodeObject(forKey: "like") as! [String]
|
81
|
+
like = aDecoder.decodeObject(forKey: "like") as! [String]
|
82
|
+
|
83
|
+
event = aDecoder.decodeObject(forKey: "event") as? String
|
84
|
+
|
85
|
+
eventName = aDecoder.decodeObject(forKey: "eventname") as? String
|
58
86
|
|
59
87
|
}
|
60
88
|
|
@@ -72,15 +100,325 @@
|
|
72
100
|
|
73
101
|
aCoder.encode(UserImage, forKey: "userimage")
|
74
102
|
|
103
|
+
aCoder.encode(lives, forKey: "lives")
|
104
|
+
|
105
|
+
aCoder.encode(sex, forKey: "sex")
|
106
|
+
|
107
|
+
aCoder.encode(age,forKey: "age")
|
108
|
+
|
109
|
+
aCoder.encode(colleage, forKey:"colleage")
|
110
|
+
|
75
111
|
aCoder.encode(like, forKey: "like")
|
76
112
|
|
113
|
+
aCoder.encode(event, forKey: "event")
|
114
|
+
|
115
|
+
aCoder.encode(eventName, forKey: "eventname")
|
116
|
+
|
77
117
|
}
|
78
118
|
|
79
119
|
}
|
80
120
|
|
81
121
|
|
82
122
|
|
123
|
+
//現在のユーザーデータと指定している本のデータを格納するシングルトン
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
final class NowUser{
|
128
|
+
|
129
|
+
static let shared = NowUser()
|
130
|
+
|
131
|
+
var nowuser = User()
|
132
|
+
|
133
|
+
var tapuser = User()
|
134
|
+
|
135
|
+
var searchSwich: Bool = true
|
136
|
+
|
137
|
+
private init() {}
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
83
|
-
```
|
143
|
+
```
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
### ソースコード(HomeViewController)
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
import UIKit
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
class HomeViewController: UIViewController ,UICollectionViewDelegate, UICollectionViewDataSource{
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
var searchUsers = [User]()
|
164
|
+
|
165
|
+
var user = User()
|
166
|
+
|
167
|
+
var collectionView: UICollectionView!
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
override func viewWillAppear(_ animated: Bool) {
|
172
|
+
|
173
|
+
LoginCheck()
|
174
|
+
|
175
|
+
searchUsers.removeAll()
|
176
|
+
|
177
|
+
tabBarController?.tabBar.isHidden = false
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
//検索用のsearchUsersと全てのユーザーを表示するusersをsearchSwichで分ける処理
|
182
|
+
|
183
|
+
if NowUser.shared.searchSwich == false {
|
184
|
+
|
185
|
+
let searchUserDefaults = UserDefaults.standard
|
186
|
+
|
187
|
+
if let storedSearchusers = searchUserDefaults.object(forKey: "searchusers") as? Data {
|
188
|
+
|
189
|
+
if let unarchivesearchusers = NSKeyedUnarchiver.unarchiveObject(with: storedSearchusers) as? [User] {
|
190
|
+
|
191
|
+
searchUsers.append(contentsOf: unarchivesearchusers)
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
NowUser.shared.searchSwich = true
|
198
|
+
|
199
|
+
}else{
|
200
|
+
|
201
|
+
let userDefaults = UserDefaults.standard
|
202
|
+
|
203
|
+
if let storedusers = userDefaults.object(forKey: "users") as? Data {
|
204
|
+
|
205
|
+
if let unarchiveusers = NSKeyedUnarchiver.unarchiveObject(with: storedusers) as? [User] {
|
206
|
+
|
207
|
+
searchUsers.append(contentsOf: unarchiveusers)
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
self.navigationItem.title = "検索"
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
collectionView.reloadData()
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
override func viewDidLoad() {
|
224
|
+
|
225
|
+
super.viewDidLoad()
|
226
|
+
|
227
|
+
view.backgroundColor = .white
|
228
|
+
|
229
|
+
//NavigationBarの設定
|
230
|
+
|
231
|
+
let searchImageButton: UIBarButtonItem = UIBarButtonItem(image: UIImage(named:"search1"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(Search(_:)))
|
232
|
+
|
233
|
+
self.navigationItem.leftBarButtonItem = searchImageButton
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
//CollectionViewの設定
|
238
|
+
|
239
|
+
let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
|
240
|
+
|
241
|
+
flowLayout.itemSize = CGSize(width: 90, height: 120)
|
242
|
+
|
243
|
+
flowLayout.minimumInteritemSpacing = 20.0
|
244
|
+
|
245
|
+
flowLayout.minimumLineSpacing = 20.0
|
246
|
+
|
247
|
+
flowLayout.sectionInset = UIEdgeInsetsMake(20,20,0,20)
|
248
|
+
|
249
|
+
collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
|
250
|
+
|
251
|
+
collectionView.backgroundColor = UIColor.white
|
252
|
+
|
253
|
+
collectionView.register(HomeCustomCell.self, forCellWithReuseIdentifier: "HomeCustomCell")
|
254
|
+
|
255
|
+
collectionView.delegate = self
|
256
|
+
|
257
|
+
collectionView.dataSource = self
|
258
|
+
|
259
|
+
self.view.addSubview(collectionView)
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
266
|
+
|
267
|
+
return searchUsers.count
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
274
|
+
|
275
|
+
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCustomCell", for: indexPath) as! HomeCustomCell
|
276
|
+
|
277
|
+
cell.userNameLabel.text = searchUsers[indexPath.row].UserName
|
278
|
+
|
279
|
+
cell.userImageView.image = UIImage(data: (searchUsers[indexPath.row].UserImage! as Data))?.resize(size: CGSize(width: 50, height: 50))
|
280
|
+
|
281
|
+
return cell
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
288
|
+
|
289
|
+
NowUser.shared.tapuser = searchUsers[indexPath.row]
|
290
|
+
|
291
|
+
navigationController?.pushViewController(ProfileViewController(), animated: true)
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
override func didReceiveMemoryWarning() {
|
298
|
+
|
299
|
+
super.didReceiveMemoryWarning()
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
@objc func LoginCheck(){
|
306
|
+
|
307
|
+
if NowUser.shared.nowuser.Email == nil{
|
308
|
+
|
309
|
+
let nc = UINavigationController(rootViewController: LoginViewController())
|
310
|
+
|
311
|
+
self.present(nc, animated: true, completion: nil)
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
@objc func Search(_ sender: UIButton){
|
320
|
+
|
321
|
+
self.present(SearchViewController(), animated: true, completion: nil)
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
class HomeCustomCell: UICollectionViewCell {
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
let userImageView: UIImageView = {
|
336
|
+
|
337
|
+
let imageView = UIImageView()
|
338
|
+
|
339
|
+
return imageView
|
340
|
+
|
341
|
+
}()
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
let userNameLabel: UILabel = {
|
346
|
+
|
347
|
+
let label = UILabel()
|
348
|
+
|
349
|
+
label.textColor = .white
|
350
|
+
|
351
|
+
label.textAlignment = NSTextAlignment.center
|
352
|
+
|
353
|
+
return label
|
354
|
+
|
355
|
+
}()
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
override func draw(_ rect: CGRect) {
|
360
|
+
|
361
|
+
self.layer.cornerRadius = 10
|
362
|
+
|
363
|
+
self.layer.masksToBounds = true
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
let viewHeight10 = self.contentView.frame.height / 10
|
368
|
+
|
369
|
+
let viewWidth22 = self.contentView.frame.width / 22
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
self.contentView.addSubview(userImageView)
|
374
|
+
|
375
|
+
self.contentView.addSubview(userNameLabel)
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
self.selectedBackgroundView = UIView(frame: self.bounds)
|
380
|
+
|
381
|
+
self.selectedBackgroundView?.backgroundColor = .red
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
userNameLabel.translatesAutoresizingMaskIntoConstraints = false
|
386
|
+
|
387
|
+
userNameLabel.topAnchor.constraint(equalTo:contentView.topAnchor,constant: viewHeight10 * 7).isActive = true
|
388
|
+
|
389
|
+
userNameLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
|
390
|
+
|
391
|
+
userNameLabel.widthAnchor.constraint(equalToConstant: viewWidth22 * 22).isActive = true
|
392
|
+
|
393
|
+
userNameLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
userImageView.translatesAutoresizingMaskIntoConstraints = false
|
398
|
+
|
399
|
+
userImageView.topAnchor.constraint(equalTo:contentView.topAnchor).isActive = true
|
400
|
+
|
401
|
+
userImageView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
|
402
|
+
|
403
|
+
userImageView.widthAnchor.constraint(equalToConstant: viewWidth22 * 22).isActive = true
|
404
|
+
|
405
|
+
userImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
|
406
|
+
|
407
|
+
}
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
```
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
|
84
422
|
|
85
423
|
### ここを読み込む時にUserクラスを参照してエラーが起こりました
|
86
424
|
|
1
修正依頼に対応しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -81,3 +81,25 @@
|
|
81
81
|
|
82
82
|
|
83
83
|
```
|
84
|
+
|
85
|
+
### ここを読み込む時にUserクラスを参照してエラーが起こりました
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
let userDefaults = UserDefaults.standard
|
92
|
+
|
93
|
+
if let storedusers = userDefaults.object(forKey: "users") as? Data {
|
94
|
+
|
95
|
+
if let unarchiveusers = NSKeyedUnarchiver.unarchiveObject(with: storedusers) as? [User] { //ここでUserクラスを参照した時にエラーが起こった
|
96
|
+
|
97
|
+
searchUsers.append(contentsOf: unarchiveusers)
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
searchUsersはUser型の配列です!( var searchUsers = [User]() )
|