我目前在Collection View Cell中创建了一个Collection View。 集合视图中的集合视图。我无法在第二个视图控制器中的didSelectItemAt
上执行segue或呈现视图控制器。现在我尝试过的所有内容都会导致崩溃,或者我在视图层次结构中看不到"视图"错误。
这是一张图片来解释:
这是我的代码:
收藏视图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
}
}
如果不清楚,我很乐意解释更多。
答案 0 :(得分:2)
您的代码存在问题:
let tab = ExploreVC()
这不是要求你当前的ExploreVC这样做,而是要求新创建的实例(这就是它失败的原因)。
要解决此问题(不修改代码太多),您需要做的是:
1)在“categoryCell”中添加一个弱变量来引用你的实际ExploreVC:
weak var exploreVC : ExploreVC?
2)在ExploreVC的“cellForItemAt”中添加对它的引用:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "category", for: indexPath) as! categoryCell
cell.exploreCategory = exploreCategories?[indexPath.item]
cell.exploreVC = self
return cell
3)在didSelectItem函数内删除“let tab = ExploreVC()”并改为添加:
exploreVC?.gotToBusinessPage()