我正在尝试在tabBarController上添加CollectionView
对于网格collectionView
所以我做了一个UICollectionViewController作为另一个快速文件
我作为UICollectionViewController.view
添加到ViewController(导航控制器)
但是当我点击collectionView的时候,细胞立刻消失了。
我想我确实跟着导游找了两天..但我不能修好它。
有些人说这是添加collectionView作为子视图的问题,但我不明白
这是自定义UICollectionView,MyFeedController.swift
import UIKit
class MyFeedController: BaseListController, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView.backgroundColor = .purple
collectionView.register(MyFeedCell.self, forCellWithReuseIdentifier: cellId)
collectionView.delegate = self
if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .vertical
}
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyFeedCell
cell.backgroundColor = .red
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
return .init(width: (screenWidth - 4 - 4) / 3, height: (screenWidth - 4 - 4) / 3)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 2
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 2
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return .init(top: 2, left: 0, bottom: 2, right: 0)
}
}
这是我的自定义单元格,MyFeedCell.swift很简单
//
// MyFeedCollectionController.swift
// My-Lord-Cat
//
// Created by Cory Kim on 17/07/2019.
// Copyright © 2019 Cory Kim. All rights reserved.
//
import UIKit
class MyFeedController: BaseListController, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView.backgroundColor = .purple
collectionView.register(MyFeedCell.self, forCellWithReuseIdentifier: cellId)
collectionView.delegate = self
if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .vertical
}
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyFeedCell
cell.backgroundColor = .red
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
return .init(width: (screenWidth - 4 - 4) / 3, height: (screenWidth - 4 - 4) / 3)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 2
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 2
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return .init(top: 2, left: 0, bottom: 2, right: 0)
}
}
所以我在MyPageController中添加了MyFeedController (这是一个UIViewController)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationController?.navigationBar.isHidden = true
setupLayout()
setupMyFeedGridCollectionView()
}
// other views are set on setupLayout()
fileprivate func setupMyFeedGridCollectionView() {
let myFeedController = MyFeedController()
view.addSubview(myFeedController.view)
myFeedController.view.anchor(top: infoStackView.bottomAnchor, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor, padding: .init(top: 10, left: 0, bottom: 0, right: 0))
// anchor method is extension for autolayout
}
发布于 2019-07-18 02:56:36
我不知道你在哪里设置集合视图的dataSource。在这一行设置一个断点,以确保它被调用:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MyFeedCell
https://stackoverflow.com/questions/57080711
复制相似问题