###前提・実現したいこと
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
回答2件
あなたの回答
tips
プレビュー