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

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

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

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

Swift

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

Q&A

解決済

2回答

466閲覧

CollectionView内の複数のフッターの処理

guestn

総合スコア13

Xcode

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

Swift

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

0グッド

0クリップ

投稿2017/10/25 06:49

###前提・実現したいこと
swiftでスタンプラリーアプリの一部を作っています。
collectionViewで構成したviewにセルが横に3つずつ並んでいてそれが2行あり
下にbuton機能を内蔵したcollection Reusable Viewを設置しています。
そのcollection Viewを一画面に二つ表示しているのですがcollection Reusable View内のbuttonの機能が現在上下両方同じ機能になっている状態です。(二つとも同じページに飛んでしまう)
cellの場合はcellForItemAt indexPathで1セクションにつき1つの挙動を
取り付けることができるのですが、collection Reusable View(フッター)の
場合はどうやって一つずつ機能を実装をしているのか分からない状態で困っています。
このような現象に遭遇したことがある方々、解決方法を知っているという方がいましたら、ぜひその意見を参考にさせていただきたいです。
写真を都合により載せることができません。ご容赦ください・・・

エラーメッセージ:なし

###該当のソースコード

import UIKit import RealmSwift class stampViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate{ @IBOutlet var tableView: UITableView! @IBOutlet weak var stampCollectionView: UICollectionView! let screenSize = UIScreen.main.bounds.size; let userDefaults = UserDefaults.standard var realm:Realm! var config = Realm.Configuration() var spotList:Results<Spot>! var spotList2:Results<Spot>! var targetSpotList: Results<Spot>! var targetSpotList2: Results<Spot>! //var targetSpotList3: Results<Spot>! override func viewDidLoad() { super.viewDidLoad() //navigationBar.title = "Stamprally" //self.navigationItem.title = "StamprallyCard" let layout = UICollectionViewFlowLayout() layout.itemSize = CGSize(width:80,height: 80)//大きさ layout.sectionInset = UIEdgeInsetsMake(16, 16, 32, 16)//マージン config.fileURL = config.fileURL!.deletingLastPathComponent() .appendingPathComponent("kokoro.realm") realm = try! Realm(configuration: self.config) //スポットの表示 let spotPredicate = NSPredicate(format:"lang =%d AND pressStamp = true AND areaNo = %d",userDefaults.integer(forKey: "lang"),userDefaults.integer(forKey: "nearAreaNo")) spotList = realm.objects(Spot.self).filter(spotPredicate).sorted(byKeyPath: "lastNotif", ascending: true) let spotPredicate2 = NSPredicate(format:"lang =%d AND pressStamp = true AND areaNo = 10",userDefaults.integer(forKey: "lang"),userDefaults.integer(forKey: "nearAreaNo")) spotList2 = realm.objects(Spot.self).filter(spotPredicate2).sorted(byKeyPath: "lastNotif", ascending: true) //エリア let areaPredicate = NSPredicate(format:"areaNo = 1") let areaList = realm.objects(Area.self).filter(areaPredicate) stampCollectionView.collectionViewLayout = layout self.setBackgroundImg() userDefaults.integer(forKey: "lang") } internal func numberOfSections(in collectionView: UICollectionView) -> Int { return 2 } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print("Num: (indexPath.row)") print("SectionNum:(indexPath.section)") } //スタンプラリー対象のスポットの数を条件を付けて算出し表示する func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { switch(section){ case 0: let stampRallyTargetSpot = getStampRallyTargetSpot(langNo: Int8(userDefaults.integer(forKey: "lang")), areaNo: Int16(userDefaults.integer(forKey: "nearAreaNo"))) //抽出されたスタンプラリースポットを格納する targetSpotList = stampRallyTargetSpot return stampRallyTargetSpot.count case 1: let stampRallyTargetSpot2 = getStampRallyTargetSpot(langNo: Int8(userDefaults.integer(forKey: "lang")), areaNo: 10) //抽出されたスタンプラリースポットを格納する targetSpotList2 = stampRallyTargetSpot2 return stampRallyTargetSpot2.count //case 2: //let stampRallyTargetSpot3 = getStampRallyTargetSpot(langNo: Int8(userDefaults.integer(forKey: "lang")), areaNo: 6) //抽出されたスタンプラリースポットを格納する //targetSpotList3 = stampRallyTargetSpot3 //return stampRallyTargetSpot3.count default: return 0 } } //フッターのレイアウト設定 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { switch(section){ case 0: return CGSize(width: self.view.frame.size.width,height: 50) case 1: return CGSize(width: self.view.frame.size.width,height: 50) //case 2: // return CGSize(width: self.view.frame.size.width,height: 80) default: return CGSize(width: self.view.frame.size.width,height: 80) } } //フッターのセルを作成 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { switch kind { case UICollectionElementKindSectionFooter: let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "StampReusable", for: indexPath as IndexPath) return footerView default: assert(false, "Unexpected element kind") } } // Cell Size Change func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { let size = UIScreen.main.bounds.size.width / 4 return CGSize(width:size,height:size) } @available(iOS 6.0, *) public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! StampViewCell switch(indexPath.section){ case 0: cell.backgroundColor = UIColor.white let baseSpot = targetSpotList[indexPath.row] //if(spotList.count > indexPath.row){ //最低でも一個スタンプラリーが存在している場合 if(spotList.count > 0){ //名前を追加していく cell.stampCellNum.text = baseSpot.vSpotName spotList.forEach{ spot in //スポットがスタンプラリー対象スポットだった場合スタンプを押す(可能な限り繰り返す) if spot.vSpotNo == baseSpot.vSpotNo { //cell.setStampPress(spot:spotList[indexPath.row]) cell.setStampPress(spot: spot) } } }else{ cell.stampCellNum.text = baseSpot.vSpotName } case 1: cell.backgroundColor = UIColor.white let baseSpot = targetSpotList2[indexPath.row] //if(spotList.count > indexPath.row){ //最低でも一個スタンプラリーが存在している場合 if(spotList2.count > 0){ //名前を追加していく cell.stampCellNum.text = baseSpot.vSpotName spotList2.forEach{ spot in //スポットがスタンプラリー対象スポットだった場合スタンプを押す(可能な限り繰り返す) if spot.vSpotNo == baseSpot.vSpotNo { //cell.setStampPress(spot:spotList[indexPath.row]) cell.setStampPress(spot: spot) } } }else{ cell.stampCellNum.text = baseSpot.vSpotName } //case 2: // cell.backgroundColor = UIColor.white //let baseSpot = targetSpotList3[indexPath.row] //if(spotList.count > indexPath.row){ //最低でも一個スタンプラリーが存在している場合 //if(spotList.count > 0){ //名前を追加していく //cell.stampCellNum.text = baseSpot.vSpotName //spotList.forEach{ spot in //スポットがスタンプラリー対象スポットだった場合スタンプを押す(可能な限り繰り返す) //if spot.vSpotNo == baseSpot.vSpotNo { //cell.setStampPress(spot:spotList[indexPath.row]) //cell.setStampPress(spot: spot) //} //} //}else{ //cell.stampCellNum.text = baseSpot.vSpotName //} default: cell.backgroundColor = UIColor.white } return cell } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func setBackgroundImg(){ UIGraphicsBeginImageContext(self.view.frame.size) UIImage(named: "paper4.jpg")?.draw(in: self.view.bounds) let image: UIImage! = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() self.view.backgroundColor = UIColor(patternImage: image) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.loadView() self.viewDidLoad() } } //self.dismiss(animated: false, completion: nil)

###試したこと
現在フッターの初期設定と高さのみ実装することができました。

###補足情報(言語/FW/ツール等のバージョンなど)
swift3.0 xcode

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

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

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

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

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

fuzzball

2017/10/25 06:58

要約すると、collectionView(_:didSelectItemAt:)の中でどっちのCollectionViewか判別したいということでしょうか?
guestn

2017/10/25 07:01

はい!まわりくどい書き方になってしまい申し訳ないです・・・
guest

回答2

0

無事解決しました。fuzzballさんの回答を参考にしてtag付けをしてログでも確認できるようにしました。しかしコレクションビューは奥が深くまだまだ操作が慣れませんね・・・(汗)

投稿2017/10/26 09:18

guestn

総合スコア13

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

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

0

ベストアンサー

collectionView(_:didSelectItemAt:)で渡されるcollectionViewで、どちらのCollectionViewか分かりますので、それで判別して下さい。

.tagを使うのが一般的です。(あらかじめ1と2を入れておく、とか)

投稿2017/10/25 07:10

fuzzball

総合スコア16731

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

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

guestn

2017/10/25 07:13

ありがとうございます。早速試してみたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問