質問編集履歴
1
基礎を見直すと解決できます。
test
CHANGED
File without changes
|
test
CHANGED
@@ -5,531 +5,3 @@
|
|
5
5
|
をcellFoeItemAtで表示したいです。
|
6
6
|
|
7
7
|
以下がRealmで定義したデータです。
|
8
|
-
|
9
|
-
```ここに言語を入力
|
10
|
-
|
11
|
-
import UIKit
|
12
|
-
|
13
|
-
import RealmSwift
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
class Tweet: Object {
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
@objc dynamic var tweetId: Int = 0
|
22
|
-
|
23
|
-
@objc dynamic var caption: String = ""
|
24
|
-
|
25
|
-
@objc dynamic var timestamp: Date = Date()
|
26
|
-
|
27
|
-
@objc dynamic var retweetCount: Int = 0
|
28
|
-
|
29
|
-
@objc dynamic var likes: Int = 0
|
30
|
-
|
31
|
-
@objc dynamic var didLike = false
|
32
|
-
|
33
|
-
@objc dynamic var replyingTo: String?
|
34
|
-
|
35
|
-
@objc dynamic var isReply: Bool { return replyingTo != nil }
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
// Userオブジェクトを取得する
|
40
|
-
|
41
|
-
let users = LinkingObjects(fromType: User.self, property: "tweets")
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
var replyTweet = List<ReplyTweet>()
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
override static func primaryKey() -> String? {
|
50
|
-
|
51
|
-
return "tweetId"
|
52
|
-
|
53
|
-
}
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
}
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
class ReplyTweet: Object {
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
@objc dynamic var replyTweetId: Int = 0
|
66
|
-
|
67
|
-
@objc dynamic var replyCaption: String = ""
|
68
|
-
|
69
|
-
@objc dynamic var replyTimeStamp: Date = Date()
|
70
|
-
|
71
|
-
@objc dynamic var replyRetweetCount: Int = 0
|
72
|
-
|
73
|
-
@objc dynamic var likes: Int = 0
|
74
|
-
|
75
|
-
@objc dynamic var didLike = false
|
76
|
-
|
77
|
-
@objc dynamic var replyingTo: String?
|
78
|
-
|
79
|
-
@objc dynamic var isReply: Bool { return replyingTo != nil }
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
let replyTweets = LinkingObjects(fromType: Tweet.self, property: "replyTweet")
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
override static func primaryKey() -> String? {
|
88
|
-
|
89
|
-
return "replyTweetId"
|
90
|
-
|
91
|
-
}
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
}
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
```
|
100
|
-
|
101
|
-
1つのツイート(Tweet)に複数のツイート(replyTweet)が入っています。
|
102
|
-
|
103
|
-
TweetCellのtweetsを介してreplyTweetを引き出しています。
|
104
|
-
|
105
|
-
TweetControllerのcellForItemAtでcellにreplyTweetを入れたいのですが、cellForItemAtの斜線に書いてあるように試してみたのですが、Tweet型にList<ReplyTweet>型を入れられないです。これはどう変換すればいいのでしょうか?
|
106
|
-
|
107
|
-
もし分かる方がおりましたらお願いします。
|
108
|
-
|
109
|
-
```ここに言語を入力
|
110
|
-
|
111
|
-
import UIKit
|
112
|
-
|
113
|
-
import RealmSwift
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
private let reuseIdentifier = "TweetCell"
|
118
|
-
|
119
|
-
private let headeerIdentifier = "TweetHeader"
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
class TweetController: UICollectionViewController {
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
// MARK: - Properties
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
private let tweets: [Tweet]
|
132
|
-
|
133
|
-
private var replies = [Tweet]() {
|
134
|
-
|
135
|
-
didSet {
|
136
|
-
|
137
|
-
collectionView.reloadData()
|
138
|
-
|
139
|
-
}
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
// MARK: - Lifecycle
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
init(tweets: [Tweet]){
|
150
|
-
|
151
|
-
self.tweets = tweets
|
152
|
-
|
153
|
-
super.init(collectionViewLayout: UICollectionViewFlowLayout())
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
required init?(coder: NSCoder) {
|
160
|
-
|
161
|
-
fatalError("init(coder:) has not been implemented")
|
162
|
-
|
163
|
-
}
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
override func viewDidLoad() {
|
168
|
-
|
169
|
-
super.viewDidLoad()
|
170
|
-
|
171
|
-
configureCollectionView()
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
func configureCollectionView(){
|
178
|
-
|
179
|
-
collectionView.backgroundColor = .white
|
180
|
-
|
181
|
-
collectionView.register(TweetCell.self, forCellWithReuseIdentifier: reuseIdentifier)
|
182
|
-
|
183
|
-
collectionView.register(TweetHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headeerIdentifier)
|
184
|
-
|
185
|
-
}
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
// MARK: - UICollectionViewDataSource
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
extension TweetController {
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
202
|
-
|
203
|
-
let realmTweet = Tweet()
|
204
|
-
|
205
|
-
return realmTweet.replyTweet.count
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
212
|
-
|
213
|
-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! TweetCell
|
214
|
-
|
215
|
-
// let realmTweet = Tweet()
|
216
|
-
|
217
|
-
// cell.tweets.replyTweet = realmTweet.replyTweet[indexPath.row]
|
218
|
-
|
219
|
-
// cell.tweets.replyTweet = replies.replyTweet
|
220
|
-
|
221
|
-
// cell.captionLabel.text = replies[indexPath.row].replyTweet
|
222
|
-
|
223
|
-
return cell
|
224
|
-
|
225
|
-
}
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
}
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
```
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
カスタムセルの内容です。
|
240
|
-
|
241
|
-
*文字数制限のため必要ないところは省略しております。
|
242
|
-
|
243
|
-
```ここに言語を入力
|
244
|
-
|
245
|
-
import UIKit
|
246
|
-
|
247
|
-
import RealmSwift
|
248
|
-
|
249
|
-
import ActiveLabel
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
protocol TweetCellDelegate: class {
|
254
|
-
|
255
|
-
func handleProfileImageTapped(_ cell: TweetCell)
|
256
|
-
|
257
|
-
func handleReplyTapped(_ cell: TweetCell)
|
258
|
-
|
259
|
-
func handleLikeTapped(_ cell: TweetCell)
|
260
|
-
|
261
|
-
func deleteActionSheet(_ cell: TweetCell)
|
262
|
-
|
263
|
-
}
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
private let realm = try! Realm()
|
268
|
-
|
269
|
-
private let userObject = Array(realm.objects(User.self))
|
270
|
-
|
271
|
-
private let tweetObject = Array(realm.objects(Tweet.self))
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
class TweetCell: UICollectionViewCell {
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
// MARK: - Properties
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
var tweets: Tweet = Tweet() {
|
284
|
-
|
285
|
-
didSet { configure() }
|
286
|
-
|
287
|
-
}
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
private var user: Results<User>!
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
weak var delegate: TweetCellDelegate?
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
private lazy var profileImageView: UIImageView = {
|
300
|
-
|
301
|
-
let iv = UIImageView()
|
302
|
-
|
303
|
-
iv.contentMode = .scaleAspectFill
|
304
|
-
|
305
|
-
iv.image = UIImage(named: "placeholderImg")
|
306
|
-
|
307
|
-
iv.clipsToBounds = true
|
308
|
-
|
309
|
-
iv.setDimensions(width: 48, height: 48)
|
310
|
-
|
311
|
-
iv.layer.cornerRadius = 48 / 2
|
312
|
-
|
313
|
-
let tap = UITapGestureRecognizer(target: self, action: #selector(handleProfileImageTapped))
|
314
|
-
|
315
|
-
iv.addGestureRecognizer(tap)
|
316
|
-
|
317
|
-
iv.isUserInteractionEnabled = true
|
318
|
-
|
319
|
-
return iv
|
320
|
-
|
321
|
-
}()
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
private let replyLabel: ActiveLabel = {
|
326
|
-
|
327
|
-
let label = ActiveLabel()
|
328
|
-
|
329
|
-
label.textColor = .lightGray
|
330
|
-
|
331
|
-
label.font = UIFont.systemFont(ofSize: 12)
|
332
|
-
|
333
|
-
label.mentionColor = .twitterBlue
|
334
|
-
|
335
|
-
return label
|
336
|
-
|
337
|
-
}()
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
public let captionLabel: ActiveLabel = {
|
342
|
-
|
343
|
-
let label = ActiveLabel()
|
344
|
-
|
345
|
-
label.font = UIFont.systemFont(ofSize: 14)
|
346
|
-
|
347
|
-
label.lineBreakMode = .byWordWrapping
|
348
|
-
|
349
|
-
label.numberOfLines = 0
|
350
|
-
|
351
|
-
label.mentionColor = .twitterBlue
|
352
|
-
|
353
|
-
label.hashtagColor = .twitterBlue
|
354
|
-
|
355
|
-
return label
|
356
|
-
|
357
|
-
}()
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
public lazy var commentButton: UIButton = {
|
362
|
-
|
363
|
-
let button = UIButton(type: .system)
|
364
|
-
|
365
|
-
button.setImage(UIImage(named: "outline_mode_comment_black_24pt_1x"), for: .normal)
|
366
|
-
|
367
|
-
button.tintColor = .darkGray
|
368
|
-
|
369
|
-
button.setDimensions(width: 20, height: 20)
|
370
|
-
|
371
|
-
button.addTarget(self, action: #selector(handleCommentTapped), for: .touchUpInside)
|
372
|
-
|
373
|
-
return button
|
374
|
-
|
375
|
-
}()
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
public lazy var retweetButton: UIButton = {
|
380
|
-
|
381
|
-
let button = UIButton(type: .system)
|
382
|
-
|
383
|
-
button.setImage(UIImage(named: "outline_autorenew_black_24pt_1x"), for: .normal)
|
384
|
-
|
385
|
-
button.tintColor = .darkGray
|
386
|
-
|
387
|
-
button.setDimensions(width: 20, height: 20)
|
388
|
-
|
389
|
-
button.addTarget(self, action: #selector(handleRetweetTapped), for: .touchUpInside)
|
390
|
-
|
391
|
-
return button
|
392
|
-
|
393
|
-
}()
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
public lazy var likeButton: UIButton = {
|
398
|
-
|
399
|
-
let button = UIButton(type: .system)
|
400
|
-
|
401
|
-
button.setImage(UIImage(named: "like_unselected"), for: .normal)
|
402
|
-
|
403
|
-
button.tintColor = .darkGray
|
404
|
-
|
405
|
-
button.setDimensions(width: 20, height: 20)
|
406
|
-
|
407
|
-
button.addTarget(self, action: #selector(handleLikeTapped), for: .touchUpInside)
|
408
|
-
|
409
|
-
return button
|
410
|
-
|
411
|
-
}()
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
public lazy var shareButton: UIButton = {
|
416
|
-
|
417
|
-
let button = UIButton(type: .system)
|
418
|
-
|
419
|
-
button.setImage(UIImage(named: "outline_share_black_24pt_1x"), for: .normal)
|
420
|
-
|
421
|
-
button.tintColor = .darkGray
|
422
|
-
|
423
|
-
button.setDimensions(width: 20, height: 20)
|
424
|
-
|
425
|
-
button.addTarget(self, action: #selector(handleShareTapped), for: .touchUpInside)
|
426
|
-
|
427
|
-
return button
|
428
|
-
|
429
|
-
}()
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
private lazy var optionButton: UIButton = {
|
434
|
-
|
435
|
-
let button = UIButton(type: .system)
|
436
|
-
|
437
|
-
button.setImage(UIImage(named: "baseline_keyboard_arrow_down_black_24pt_1x-1"), for: .normal)
|
438
|
-
|
439
|
-
button.tintColor = .lightGray
|
440
|
-
|
441
|
-
button.addTarget(self, action: #selector(showActionSheet), for: .touchUpInside)
|
442
|
-
|
443
|
-
return button
|
444
|
-
|
445
|
-
}()
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
public let infoLabel = UILabel()
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
var tweetObject = Tweet()
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
var usernameText: String {
|
458
|
-
|
459
|
-
return "@(userObject[0].username)"
|
460
|
-
|
461
|
-
}
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
var timestamp: String {
|
466
|
-
|
467
|
-
let formatter = DateComponentsFormatter()
|
468
|
-
|
469
|
-
formatter.allowedUnits = [.second, .minute, .hour, .day, .weekOfMonth]
|
470
|
-
|
471
|
-
formatter.maximumUnitCount = 1
|
472
|
-
|
473
|
-
formatter.unitsStyle = .abbreviated
|
474
|
-
|
475
|
-
let now = Date()
|
476
|
-
|
477
|
-
return formatter.string(from: tweetObject.timestamp, to: now) ?? "2m"
|
478
|
-
|
479
|
-
}
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
var headerTimeStamp: String {
|
484
|
-
|
485
|
-
let formatter = DateFormatter()
|
486
|
-
|
487
|
-
formatter.dateFormat = "h:mm a ・ MM/dd/yyyy"
|
488
|
-
|
489
|
-
return formatter.string(from: tweetObject.timestamp)
|
490
|
-
|
491
|
-
}
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
// MARK: - Helper
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
public func configure(){
|
500
|
-
|
501
|
-
if userObject[0].profileImage != nil {
|
502
|
-
|
503
|
-
self.profileImageView.image = UIImage(data: userObject[0].profileImage!)
|
504
|
-
|
505
|
-
} else {
|
506
|
-
|
507
|
-
profileImageView.image = UIImage(named: "placeholderImg")
|
508
|
-
|
509
|
-
}
|
510
|
-
|
511
|
-
}
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
func createButton(withImageName imageName: String) -> UIButton {
|
516
|
-
|
517
|
-
let button = UIButton(type: .system)
|
518
|
-
|
519
|
-
button.setImage(UIImage(named: imageName), for: .normal)
|
520
|
-
|
521
|
-
button.tintColor = .darkGray
|
522
|
-
|
523
|
-
button.setDimensions(width: 20, height: 20)
|
524
|
-
|
525
|
-
return button
|
526
|
-
|
527
|
-
}
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
}
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
```
|