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

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

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

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

475閲覧

AnchorConstraintやdequeueReusableCellの定義について教えてください。

退会済みユーザー

退会済みユーザー

総合スコア0

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2018/02/27 07:37

編集2018/02/28 08:20

ストーリーボード未使用で、コードのみで画像のビューを実装しました。
コードの成り立ちが自分で理解できているかを確認したいです。

いくつか気になる部分を見ていただきたいので、
誤りがある場合はご指導お願いいたします。(AppDelegateは省いています。)

イメージ説明

・WordCellクラス内にsetupViews()をインスタンス化
WordCellの四方のアンカーがテキストによって変動可能な様にisActiveをtrueにする、アンカーに対応できるCGRectをframe:に指定。

・グローバルスコープで各Id(cell,header,footer)を定義して、
この3つを登録=registerしないと反映されないので、
registerでは、cellはWordCell、headerとfooterはUICollectionViewCellを指定。他と干渉しない様にselfをつける。
collectionViewの要素として定義するパターンで、UICollectionElementKindSectionHeader,hooterを選択、各Idも指定。

・if kind == では、header,footer両方一緒にdequeueできる形のif文を使用。

import UIKit //ワードセル class WordCell: UICollectionViewCell { //this gets called when a cell is dequeued override init(frame: CGRect) { super.init(frame: frame) setupViews() } let wordLabel: UILabel = { let label = UILabel() label.text = "TEST TEST TEST" label.translatesAutoresizingMaskIntoConstraints = false return label }() func setupViews() { backgroundColor = .yellow addSubview(wordLabel) wordLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true wordLabel.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true wordLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true wordLabel.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } //コントローラー セルID class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { let cellId = "cellId" let headerId = "headerId" let footerId = "footerId" override func viewDidLoad() { super.viewDidLoad() collectionView?.backgroundColor = .white collectionView?.register(WordCell.self, forCellWithReuseIdentifier: cellId) collectionView?.register(UICollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerId) collectionView?.register(UICollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: footerId) } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 4 } //セル override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) return cell } //セル func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: view.frame.width, height: 50) } //ヘッダー override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { //if ヘッダー フッター if kind == UICollectionElementKindSectionHeader { let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) header.backgroundColor = .blue return header } else { let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: footerId, for: indexPath) footer.backgroundColor = . green return footer } } //ヘッダー func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSize(width: view.frame.width, height: 50) } //フッター func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { return CGSize(width: view.frame.width, height: 100) } }

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

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

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

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

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

nakasho_dev

2018/02/28 11:26

確認したいところは箇条書きで提示されていることでしょうか。「誤りがない」ということを確認するのは非常に負担がかかることです。何を回答者に求めているか具体的に提示されてはいかがでしょうか。
guest

回答1

0

ベストアンサー

不明確な質問でした。
もっと今よりも理解を深めて、具体的に不明点があった場合に質問させて頂きたいと思います。
ありがとうございます。

投稿2018/02/28 15:00

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問