質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

359閲覧

UILabelについて

totot

総合スコア20

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2018/12/31 01:48

編集2018/12/31 02:05

チャットアプリを模して作りました。
nameLabelにMark Zuckerbergと書いて時間の左、メッセージの上に表示しようとしたのですが、上手く表示されません。
エラーは出ていません。mark.name = "Mark Zuckerberg"と書いたのになぜnameLabelがnilになってしまうのでしょうか?

スクリーンショットです。

Swift

1import UIKit 2 3class Friend: NSObject{ 4 5 var name: String? 6 var profileImageName: String? 7} 8 9class Message: NSObject { 10 11 var text: String? 12 var date: NSData? 13 14 var friend: Friend? 15} 16 17class FriendsController: UICollectionViewController, UICollectionViewDelegateFlowLayout{ 18 19 var messages: [Message]? 20 21 func setupData(){ 22 23 let mark = Friend() 24 mark.name = "Mark Zuckerberg" 25 mark.profileImageName = "profile2" 26 27 let message = Message() 28 message.text = "Hello, my name is Mark. Nice to meet you" 29 message.date = NSData() 30 31 messages = [message] 32 } 33 34 override func viewDidLoad() { 35 super.viewDidLoad() 36 navigationItem.title = "Recent" 37 collectionView.backgroundColor = UIColor.white 38 collectionView.alwaysBounceVertical = true 39 self.collectionView.register(MessageCell.self, forCellWithReuseIdentifier: "cellId") 40 41 setupData() 42 } 43 44 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 45 if let count = messages?.count{ 46 return count 47 } 48 return 0 49 } 50 51 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 52 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! MessageCell 53 54 if let message = messages?[indexPath.item] { 55 cell.message = message 56 } 57 return cell 58 } 59 60 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 61 return CGSize(width: view.frame.width, height: 100) 62 } 63} 64 65class MessageCell: BaseCell{ 66 67 var message: Message? { 68 didSet { 69 nameLabel.text = message?.friend?.name 70 71 if let image = message?.friend?.profileImageName { 72 profileImage.image = UIImage(named: image) 73 } 74 75 messageLabel.text = message?.text 76 } 77 } 78 79 let profileImage: UIImageView = { 80 let imageView = UIImageView() 81 imageView.contentMode = .scaleAspectFill 82 imageView.image = UIImage(named: "profile1") 83 imageView.layer.cornerRadius = 34 84 imageView.layer.masksToBounds = true 85 return imageView 86 }() 87 88 let dividerLineView: UIView = { 89 let view = UIView() 90 view.backgroundColor = UIColor(white: 0.5, alpha: 0.5) 91 return view 92 }() 93 94 let nameLabel: UILabel = { 95 let label = UILabel() 96 label.text = "あああああああ" 97 label.font = UIFont.systemFont(ofSize: 18) 98 return label 99 }() 100 101 let messageLabel: UILabel = { 102 let label = UILabel() 103 label.text = "your friend's message and something else..." 104 label.textColor = UIColor.darkGray 105 label.font = UIFont.systemFont(ofSize: 14) 106 return label 107 }() 108 109 let timeLabel: UILabel = { 110 let label = UILabel() 111 label.text = "12:05 pm" 112 label.font = UIFont.systemFont(ofSize: 16) 113 label.textAlignment = .right 114 return label 115 }() 116 117 let hasReadImageView: UIImageView = { 118 let imageView = UIImageView() 119 imageView.contentMode = .scaleAspectFill 120 imageView.image = UIImage(named: "profile1") 121 imageView.layer.cornerRadius = 10 122 imageView.layer.masksToBounds = true 123 return imageView 124 }() 125 126 override func setupViews(){ 127 addSubview(profileImage) 128 addSubview(dividerLineView) 129 130 setupContainerView() 131 132 addConstraint(NSLayoutConstraint(item: profileImage, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)) 133 134 addConstraintsWithFormat(format: "H:|-12-[v0(68)]", views: profileImage) 135 addConstraintsWithFormat(format: "V:[v0(68)]", views: profileImage) 136 137 addConstraintsWithFormat(format: "H:|-82-[v0]|", views: dividerLineView) 138 addConstraintsWithFormat(format: "V:[v0(1)]|", views: dividerLineView) 139 } 140 141 private func setupContainerView(){ 142 143 let containerView = UIView() 144 addSubview(containerView) 145 146 addConstraintsWithFormat(format: "H:|-90-[v0]|", views: containerView) 147 addConstraintsWithFormat(format: "V:[v0(50)]", views: containerView) 148 addConstraint(NSLayoutConstraint(item: containerView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)) 149 150 containerView.addSubview(nameLabel) 151 containerView.addSubview(messageLabel) 152 containerView.addSubview(timeLabel) 153 containerView.addSubview(hasReadImageView) 154 155 addConstraintsWithFormat(format: "H:|[v0][v1(80)]-12-|", views: nameLabel, timeLabel) 156 addConstraintsWithFormat(format: "V:|[v0][v1(24)]|", views: nameLabel, messageLabel) 157 addConstraintsWithFormat(format: "H:|[v0]-8-[v1(20)]-12-|", views: messageLabel, hasReadImageView) 158 addConstraintsWithFormat(format: "V:|[v0(24)]-8-[v1(20)]", views: timeLabel, hasReadImageView) 159 } 160} 161 162class BaseCell: UICollectionViewCell { 163 164 override init(frame: CGRect) { 165 super.init(frame: frame) 166 setupViews() 167 func setupViews(){ 168 169 } 170   required init?(coder aDecoder: NSCoder) { 171 fatalError("init(coder:) has not been implemented") 172 } 173 174}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2018/12/31 02:00

で、しつもんはなんでしょうか
totot

2018/12/31 02:07

質問内容を修正しました。
hameji001

2019/01/01 08:14

みてみようと思いましたが、collectionviewに疎く、 状況を再現できませんでした。 動く簡易版などをgithubなどにuploadしていただけないでしょうか?
guest

回答1

0

ベストアンサー

以下の処理だけが原因か分かりませんが、少なくともmessage.friend = markの行がないのでデータにFrindが設定されていませんね。

swift

1 2func setupData(){ 3 4 let mark = Friend() 5 mark.name = "Mark Zuckerberg" 6 mark.profileImageName = "profile2" 7 8 let message = Message() 9 message.text = "Hello, my name is Mark. Nice to meet you" 10 message.date = NSData() 11 12 message.friend = mark 13 messages = [message] 14}

投稿2019/01/01 08:58

_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

totot

2019/01/01 10:03

回答ありがとうございます。レベルの低い質問でしたが、今の今まで気づきませんでした。本当にありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問