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

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

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

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

Q&A

0回答

823閲覧

プロフィール画像が表示されるページとされないページがある

amazon_106

総合スコア50

Swift

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

0グッド

0クリップ

投稿2019/05/17 02:13

勉強としてInstagramに似たアプリを作っています。

新しく作ったFeedページ
イメージ説明

以前、作ったユーザーページ
イメージ説明
赤で塗りつぶしたところには名前が入っています。

以下のコードでFeedページのアイコンと名前、postされた投稿やコメントが表示されると思うのですが、読み込めれません。何故なのかわからず、1週間止まっています。
どなたかお分かりになられる方、ご回答のほどよろしくお願いします。

// FeedVC.swift import UIKit import Firebase private let reuseIdentifier = "Cell" class FeedVC: UICollectionViewController, UICollectionViewDelegateFlowLayout { // MARK: -Properties var posts = [Post]() override func viewDidLoad() { super.viewDidLoad() collectionView.backgroundColor = .white // register cell classes self.collectionView!.register(FeedCell.self, forCellWithReuseIdentifier: reuseIdentifier) //configure logout button configureNavigationBar() // fetch posts fetchPosts() } // MARK: - UICollectionViewFlowLayout func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // UI部分 文字数カウントに引っかかったので省略 } // MARK: - UICollectionViewDataSource override func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return posts.count } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! FeedCell cell.post = posts[indexPath.item] return cell } // MARK: -HandIers @objc func handleShowMessages() { print("Handle show messages") } func configureNavigationBar(){ self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout)) self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "send2"), style: .plain, target: self, action: #selector(handleShowMessages)) self.navigationItem.title = "Feed" } @objc func handleLogout(){ let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) / alertController.addAction(UIAlertAction(title: "Log Out", style: .destructive, handler: { (_) in do { try Auth.auth().signOut() let loginVC = LoginVC() let navController = UINavigationController(rootViewController: loginVC) self.present(navController, animated: true, completion: nil) print("Successfully logged user out") } catch { // hanle error print("Failed to sign out") } })) alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) present(alertController, animated: true, completion: nil) } // MARK: - API func fetchPosts() { POSTS_REF.observe(.childAdded) { (snapshot) in let postId = snapshot.key guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return } let post = Post(postId: postId, dictionary: dictionary) self.posts.append(post) self.posts.sort(by: { (post1, post2) -> Bool in return post1.creationDate > post2.creationDate }) print("Post caption is ",post.caption) self.collectionView?.reloadData() } } }
// FeedCell.swift import UIKit import Firebase class FeedCell: UICollectionViewCell { var post: Post? { didSet { guard let ownerUid = post?.ownerUid else { return } guard let imageUrl = post?.imageUrl else { return } guard let likes = post?.likes else { return } Database.fetchUser(with: ownerUid) { (user) in self.profileImageView.loadImage(with: user.profileImageUrl) self.usernameButton.setTitle(user.username, for: .normal) self.configurePostCaption(user: user) } postImageView.loadImage(with: imageUrl) likesLabel.text = "(likes) likse" } } let profileImageView: CustomImageView = { let iv = CustomImageView() iv.contentMode = .scaleAspectFill iv.clipsToBounds = true iv.backgroundColor = .lightGray return iv }() let usernameButton: UIButton = { let button = UIButton(type: .system) button.setTitle("Username", for: .normal) button.setTitleColor(.black, for: .normal) button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 12) return button }() let optionsButton: UIButton = { // UI部分 文字数カウントに引っかかったので省略 }() let postImageView: CustomImageView = { let iv = CustomImageView() iv.contentMode = .scaleAspectFill iv.clipsToBounds = true iv.backgroundColor = .lightGray return iv }() let likeButton: UIButton = { // UI部分 文字数カウントに引っかかったので省略 }() let commentButton: UIButton = { // UI部分 文字数カウントに引っかかったので省略 }() let messageButton: UIButton = { // UI部分 文字数カウントに引っかかったので省略 }() let savePostButton: UIButton = { // UI部分 文字数カウントに引っかかったので省略 }() let likesLabel: UILabel = { let label = UILabel() label.font = UIFont.boldSystemFont(ofSize: 12) label.text = "3 likes" return label }() let captionLabel: UILabel = { let label = UILabel() let attributedText = NSMutableAttributedString(string: "Username", attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]) attributedText.append(NSAttributedString(string: " Some test caption for now", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12)])) label.attributedText = attributedText return label }() let postTimeLabel: UILabel = { // UI部分 文字数カウントに引っかかったので省略 }() override init(frame: CGRect) { super.init(frame: frame) addSubview(profileImageView) profileImageView.anchor(top: topAnchor, left: leftAnchor, bottom: nil, right: nil, paddingTop: 8, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 40, height: 40) profileImageView.layer.cornerRadius = 40 / 2 addSubview(usernameButton) usernameButton.anchor(top: nil, left: profileImageView.rightAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) usernameButton.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true addSubview(optionsButton) optionsButton.anchor(top: nil, left: nil, bottom: nil, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 8, width: 0, height: 0) optionsButton.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true addSubview(postImageView) postImageView.anchor(top: profileImageView.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 8, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) postImageView.heightAnchor.constraint(equalTo: widthAnchor, multiplier: 1).isActive = true configureActionButtons() addSubview(likesLabel) likesLabel.anchor(top: likeButton.bottomAnchor, left: leftAnchor, bottom: nil, right: nil, paddingTop: -4, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) addSubview(captionLabel) captionLabel.anchor(top: likesLabel.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 8, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 0) addSubview(postTimeLabel) postTimeLabel.anchor(top: captionLabel.bottomAnchor, left: leftAnchor, bottom: nil, right: nil, paddingTop: 8, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) } func configurePostCaption(user: User) { guard let post = self.post else { return } guard let caption = post.caption else { return } let attributedText = NSMutableAttributedString(string: user.username, attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12)]) attributedText.append(NSAttributedString(string: " (caption)", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12)])) captionLabel.attributedText = attributedText } func configureActionButtons() { let stackView = UIStackView(arrangedSubviews: [likeButton, commentButton, messageButton]) stackView.axis = .horizontal stackView.distribution = .fillEqually addSubview(stackView) // UI部分 文字数カウントに引っかかったので省略 addSubview(savePostButton) // UI部分 文字数カウントに引っかかったので省略 } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }

示すコードで足りないところやその他ご指摘あればコメントして頂けると助かります。

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

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

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

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

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

fuzzball

2019/05/17 02:35 編集

一部コードを省略しているようですが、この省略されたコードでも質問のような症状が発生しますか?
amazon_106

2019/05/17 02:57

はい、これが発生したコードをそのままコピペしたら、文字数オーバーになってしまったので、省略しました。 的を得た返信でなかったら、すみません。
fuzzball

2019/05/17 03:00

いえ、そういうことではなくて、省略したコードに原因が含まれている可能性はありませんか?という意味です。
amazon_106

2019/05/17 03:06

すいません、FeedVC.swiftはheightやwidthで、FeedCell.swiftはanchor部分なので、可能性はないと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問