2017-07-21 9 views
1

私は現在、コレクションビューセル内にコレクションビューを作成しています。 コレクションビュー内のコレクションビュー。セグを実行したり、Second ControllerのdidSelectItemAtにView Controllerを表示するのに問題があります。現在私が試したことのすべてがクラッシュしたり、「ビュー階層にはないビュー」エラーが表示されます。ここでCollectionViewセルからプログラムでSegueを実行する

は説明するための画像です:

ここ

enter image description here

は私のコードです:

コレクションビュー1:

class ExploreVC: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 


var businessView: UIViewController! 

let foodCategory = exploreCategory() 
let shopCategory = exploreCategory() 
let cultureCategory = exploreCategory() 
let nightlifeCategory = exploreCategory() 

@IBOutlet weak var collectionView: UICollectionView! 



override func viewWillAppear(_ animated: Bool) { 
    collectionView.delegate = self 
    collectionView.dataSource = self 
    collectionView.backgroundColor = UIColor.clear 
    collectionView.register(categoryCell.self, forCellWithReuseIdentifier: "category") 

    exploreCategories = exploreCategory.setCategories() 
    businessView = storyboard?.instantiateViewController(withIdentifier: "businessPage") 

} 




func setCategories() -> [exploreCategory]{ 


    foodCategory.name = "Good Food" 


    shopCategory.name = "Shopping Near You" 


    cultureCategory.name = "Cultural Highlights" 


    nightlifeCategory.name = "Start Your Night" 



    return [foodCategory,shopCategory,cultureCategory,nightlifeCategory] 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "category", for: indexPath) as! categoryCell 
    cell.exploreCategory = exploreCategories?[indexPath.item] 
    return cell 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    if let count = exploreCategories?.count { 

     return count 
    } 

    return 0 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    let size = CGSize(width: view.frame.width, height: 280) 

    return size 
} 

func gotToBusinessPage() { 


    print("gotToBusinessPage is being executed") 
    let top: UIViewController? = 
UIApplication.shared.keyWindow?.rootViewController 
    let businessPage = BusinessVC() 
    top?.performSegue(withIdentifier: "toBusiness", sender: self) 
    //top?.present(businessPage, animated: true) { _ in } 

    //present(businessPage, animated: true) 

} 

コレクションビュー1セル:

import UIKit 
import Foundation 
import AlgoliaSearch 
import SwiftyJSON 
import AFNetworking 

class categoryCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

private let cellId = "exploreCell" 

//* MARK Algolia 
var exploreSearch = [exploreBusiness]() 
var businessIndex: AlgoliaSearch.Index! 
let query = Query() 
var searchId = 0 
var displayedSearchId = -1 
var loadedPage: UInt = 0 
var nbPages: UInt = 0 


//*MARK Set Cell Values 
var exploreCategory: exploreCategory? { 

    didSet { 

     if let name = exploreCategory?.name { 

      categoryLabel.text = name.uppercased() 


     } 

    } 

} 


//*MARK Init 
override init(frame: CGRect) { 
    super.init(frame: frame) 
    setUpViews() 
    searchFood(category: (exploreCategories?[0])!) 
    searchShop(category: (exploreCategories?[1])!) 
    searchCulture(category: (exploreCategories?[2])!) 
    searchNightlife(category: (exploreCategories?[3])!) 
} 


required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 


//*MARK UI Mark Up 
let categoryLabel: UILabel = { 

    let black = UIColor(red:0.29, green:0.29, blue:0.29, alpha:1.0) 
    let label = UILabel() 
    label.font = UIFont(name: "AvenirNext-DemiBold", size: 14) 
    label.textColor = black 
    label.text = "TRENDING NEAR YOU" 
    label.translatesAutoresizingMaskIntoConstraints = false 
    label.numberOfLines = 1 

    return label 

}() 

let businessCollectionView: UICollectionView = { 

    let layout = UICollectionViewFlowLayout() 
    layout.scrollDirection = .horizontal 

    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) 
    collectionView.translatesAutoresizingMaskIntoConstraints = false 
    collectionView.backgroundColor = UIColor.clear 
    return collectionView 

}() 



//* MARK UI Set Up 
func setUpViews(){ 


    addSubview(businessCollectionView) 
    addSubview(categoryLabel) 

    businessCollectionView.dataSource = self 
    businessCollectionView.delegate = self 
    businessCollectionView.showsHorizontalScrollIndicator = false 


    businessCollectionView.register(exploreCell.self, forCellWithReuseIdentifier: cellId) 


    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": categoryLabel])) 

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": businessCollectionView])) 

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[categoryLabel(24)][v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": businessCollectionView, "categoryLabel": categoryLabel])) 

} 



//*MARK CollectionView Set Up 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    if let count = exploreCategory?.businesses?.count { 

     return count 

    } 

    return 0 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! exploreCell 
    cell.business = exploreCategory?.businesses?[indexPath.item] 


    return cell 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    let size = CGSize(width: 124, height: frame.height - 30) 

    return size 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 
    return UIEdgeInsetsMake(0, 16, 0, 16) 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! exploreCell 
    cell.business = exploreCategory?.businesses?[indexPath.item] 


    let cellData = cell.business 
    businessName = (cellData?.businessName!)! 
    businessID = (cellData?.objectID!)! 
    businessType = (cellData?.businessType!)! 
    zipcode = (cellData?.zipcode!)! 
    state = (cellData?.state!)! 
    address = (cellData?.address!)! 
    city = (cellData?.city!)! 
    hours = "\(cellData?.openTime! ?? "error") - \(cellData?.closeTime! ?? "error")" 
    if cellData?.image1URL?.isEmpty == false { 

     image1 = (cellData?.image1URL!)! 
     images.append(image1) 

    } 

    if cellData?.image2URL?.isEmpty == false { 

     image2 = (cellData?.image2URL!)! 
     images.append(image2) 
    } 

    if cellData?.image3URL?.isEmpty == false { 

     image3 = (cellData?.image3URL!)! 
     images.append(image3) 
    } 

    if cellData?.image4URL?.isEmpty == false { 

     image4 = (cellData?.image4URL!)! 
     images.append(image4) 
    } 

    if cellData?.image5URL?.isEmpty == false { 

     image5 = (cellData?.image5URL!)! 
     images.append(image5) 
    } 

    if cellData?.image6URL?.isEmpty == false { 

     image6 = (cellData?.image6URL!)! 
     images.append(image6) 
    } 

    if cellData?.image7URL?.isEmpty == false { 

     image7 = (cellData?.image7URL!)! 
     images.append(image7) 
    } 



    let tab = ExploreVC() 

    tab.gotToBusinessPage() 



    } 
} 

第二コレクションビューのセル:

import UIKit 
import AlamofireImage 
import Alamofire 

class exploreCell: UICollectionViewCell { 

weak var exploreVC : ExploreVC? 
var mainVC = ExploreVC() 

var business: exploreBusiness? { 

    didSet { 


     nameLabel.text = business?.businessName 
     reviewLabel.text = "\(business?.reviewCount! ?? 0) Reviews" 

     let url = URL(string: (business?.image1URL)!) 
     Alamofire.request(url!).responseImage { response in 


      if let image = response.result.value { 

       self.imageView.image = image 


      } 
     } 


    } 

} 



override init(frame: CGRect) { 
    super.init(frame: frame) 
    setUpViews() 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 

let imageView: UIImageView = { 
let iv = UIImageView() 

    iv.contentMode = .scaleAspectFill 
    iv.layer.cornerRadius = 4 
    iv.layer.masksToBounds = true 
    return iv 

}() 

let nameLabel: UILabel = { 

    let black = UIColor(red:0.29, green:0.29, blue:0.29, alpha:1.0) 
    let label = UILabel() 
    label.text = "Paradise Diner" 
    label.font = UIFont(name: "AvenirNext-DemiBold", size: 14) 
    label.textColor = black 
    label.numberOfLines = 2 
    return label 

}() 

let reviewIcon: UIImageView = { 
    let iv = UIImageView() 
    iv.image = #imageLiteral(resourceName: "review-black") 
    iv.contentMode = .scaleAspectFill 
    iv.layer.masksToBounds = true 
    return iv 

}() 

let reviewLabel: UILabel = { 

    let black = UIColor(red:0.29, green:0.29, blue:0.29, alpha:1.0) 
    let label = UILabel() 
    label.text = "45 Reviews" 
    label.font = UIFont(name: "AvenirNext-DemiBold", size: 14) 
    label.textColor = black 
    label.numberOfLines = 1 
    return label 

}() 


func setUpViews(){ 

    addSubview(imageView) 
    addSubview(nameLabel) 
    addSubview(reviewIcon) 
    addSubview(reviewLabel) 

    let ImageRect = CGRect(x: 0, y: 0, width: frame.width, height: 182) 
    let nameLabelRect = CGRect(x: 0, y: 190 , width: frame.width, height: 20) 
    let reviewRect = CGRect(x: 0, y: 220 , width: 16, height: 11) 
    let reviewLabelRect = CGRect(x: 22, y: 217 , width: frame.width - 16, height: 16) 



    imageView.frame = ImageRect 
    nameLabel.frame = nameLabelRect 
    reviewIcon.frame = reviewRect 
    reviewLabel.frame = reviewLabelRect 

    } 

} 

私はこれが明確でない場合は、よりを説明させていただきます。これは、(それが失敗した理由である)それを行う代わりに、それを行うために、その求めて新たに作成されたインスタンスを現在のExploreVCを求めていない

let tab = ExploreVC() 

+0

CollectionView Delegateメソッドは、セル内ではなく、コントローラ「ExploreVC」になければなりません。 –

+0

コレクションビュー内のコレクションビューであるため、デリゲートメソッドがセルにあります – ethanfox27

答えて

2

あなたのコードの問題は、このラインです。あなたは(あなたのコードを変更せずに、あまりにも多くを)これを修正するために何をすべきか

は次のとおりです。

1)実際のExploreVCを参照するために、あなたの「categoryCell」の内側に弱い変数を追加します。

weak var exploreVC : ExploreVC? 

"聞かせてタブ= ExploreVCを()" を削除し、代わりにこれを追加しdidSelectItem関数の中

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "category", for: indexPath) as! categoryCell 
cell.exploreCategory = exploreCategories?[indexPath.item] 
cell.exploreVC = self 
return cell 

3):2)あなたのExploreVCから "cellForItemAt" 内のそれへの参照を追加します。

exploreVC?.gotToBusinessPage() 
+0

2番目のコレクションビューのセルからビューコントローラを実行または表示しようとしています。 (黒いセル) – ethanfox27

+0

次に、そのリファレンスを渡してください。ポイントは、あなたが行っていたように新しいインスタンスではなく、あなたのExploreVCがセグを実行しなければならないということです。 – Pochi

+0

categoryCellクラスの場合は、どのようにしてselfに設定できますか?あるいは、私はそれを自分の中に置くことができますか? – ethanfox27

関連する問題