UICollectionView 两个具有相同视图的单元格。第二个单元格不显示任何内容
Posted
技术标签:
【中文标题】UICollectionView 两个具有相同视图的单元格。第二个单元格不显示任何内容【英文标题】:UICollectionView two cells with same views. Second cell shows nothing 【发布时间】:2020-01-02 14:25:07 【问题描述】:什么会导致这种行为?我有一个带有两个单元格的集合视图。这两个单元格应具有相同的视图但不同的文本内容。第二个单元格中没有显示任何内容。我在“cellforrowat”中做错了吗?我错过了什么?
class UpgradeController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource,UIScrollViewDelegate
let numMonths = ["1","6","12"]
let months = ["month","months","months"]
let prices = ["$19.99","$79.99","$99.99"]
let pricePerMonth = ["","($13.33 per month)","($8.33 per month)"]
let pricesExtra = ["$29.99","119.99","149.99"]
let pricePerMonthExtra = ["","$19.99 per month","$12.49 per month"]
var collectionView:UICollectionView!
var scrollView:UIScrollView!
let cellId = "cellId"
let cellId2 = "cellId2"
override func viewDidLoad()
super.viewDidLoad()
self.setupViews()
func setupViews()
let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
let navBarHeight: CGFloat = self.navigationController!.navigationBar.frame.height
let tabBarheight: CGFloat = self.tabBarController!.tabBar.frame.height
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
//view.setGradientBackgroundColor(colorOne: UIColor(rgb:0x000000), colorTwo: UIColor(rgb:0x056644))
navigationController?.navigationBar.isTranslucent = false
setupCollectionView()
setupMenuBar()
scrollView = UIScrollView()
scrollView.backgroundColor = .clear //.orange
scrollView.delegate = self
scrollView.frame = CGRect(x:0,y:50,width:UIScreen.main.bounds.width,height:UIScreen.main.bounds.height)
if let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout
flowLayout.scrollDirection = .horizontal //= .horizontal
scrollView.addSubview(self.collectionView)
self.view.addSubview(scrollView)
集合视图设置:
func setupCollectionView()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 120, right: 0)
//layout.itemSize = CGSize(width: screenWidth / 3, height: screenWidth / 3)
//layout.itemSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.collectionViewLayout = layout
collectionView.dataSource = self
collectionView.delegate = self
collectionView?.register(DragonCell.self, forCellWithReuseIdentifier: cellId)
collectionView!.backgroundColor = UIColor.clear
collectionView.contentInset = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 0)
collectionView.isPagingEnabled = true
self.view.addSubview(collectionView)
let titles = ["Dragon", "DragonExtra"]
lazy var menuBar: MenuBar =
let mb = MenuBar()
mb.backgroundColor = .red
mb.translatesAutoresizingMaskIntoConstraints = false
mb.names = ["Dragon", "DragonExtra"]
mb.upgradeController = self
return mb
()
private func setupMenuBar()
menuBar.setupHorizontalBar()
//menuBar.multiplier = CGFloat(1/8.0)
view.addSubview(menuBar) //view.addSubview(menuBar)
view.addConstraintsWithFormat("H:[v0(\(view.frame.width*CGFloat(menuBar.names.count)/4.0))]", views: menuBar)
view.addConstraintsWithFormat("V:|[v0(50)]|", views: menuBar)
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[v1]-(<=1)-[v0]", options: NSLayoutConstraint.FormatOptions.alignAllCenterX, metrics: nil, views: ["v0":menuBar,"v1":self.view])) //center horizontally
func scrollToMenuIndex(menuIndex:Int)
let indexPath = NSIndexPath(item: menuIndex, section: 0)
collectionView.scrollToItem(at: indexPath as IndexPath, at: [], animated: true)
//setTitleForIndex(index:menuIndex)
func scrollViewDidScroll(_ scrollView: UIScrollView)
//menuBar.horizontalBarLeftAnchorConstraint?.constant = scrollView.contentOffset.x/2 + view.frame.width/8.0
menuBar.horizontalBarLeftAnchorConstraint?.constant = scrollView.contentOffset.x/CGFloat(titles.count*2)
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
let index = targetContentOffset.pointee.x / view.frame.width
let indexPath = NSIndexPath(item: Int(index), section: 0)
menuBar.collectionView.selectItem(at: indexPath as IndexPath, animated: true, scrollPosition: [])
//setTitleForIndex(index:Int(index))
private func setTitleForIndex(index:Int)
/*
if let titleLabel = navigationItem.titleView as? UILabel
titleLabel.text = titles[Int(index)]
*/
集合视图委托方法:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
return 2
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
print(indexPath.item)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! DragonCell
switch indexPath.item
case 0:
cell.numMonths = numMonths
cell.months = months
cell.prices = prices
cell.pricePerMonth = pricePerMonth
cell.backgroundColor = .blue
return cell
case 1:
cell.numMonths = numMonths
cell.months = months
cell.prices = pricesExtra
cell.pricePerMonth = pricePerMonthExtra
cell.backgroundColor = .red
return cell
default:
let cell = UICollectionViewCell() //collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
return cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
return CGSize(width:view.frame.width,height:view.frame.height)
【问题讨论】:
【参考方案1】:根据我在您的代码中看到的内容,您希望重用相同的集合视图但只是更改内容?
那么为什么不根据选择的菜单栏项来设置条件呢?
像这样……
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
print(indexPath.item)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! DragonCell
//Not exactly sure if this is correct for what you have written
//for your code. But the idea is take the index that is shown or active //from your menu bar.
//You have used a switch statement which used the collection view //index. not the menubar index
if menuBar.index == 0
cell.numMonths = numMonths
cell.months = months
cell.prices = prices
cell.pricePerMonth = pricePerMonth
cell.backgroundColor = .blue
return cell
else
cell.numMonths = numMonths
cell.months = months
cell.prices = pricesExtra
cell.pricePerMonth = pricePerMonthExtra
cell.backgroundColor = .red
return cell
//switch indexPath.item
//case 0:
//cell.numMonths = numMonths
//cell.months = months
//cell.prices = prices
//cell.pricePerMonth = pricePerMonth
//cell.backgroundColor = .blue
//return cell
//case 1:
//cell.numMonths = numMonths
//cell.months = months
//cell.prices = pricesExtra
//cell.pricePerMonth = pricePerMonthExtra
//cell.backgroundColor = .red
//return cell
//default:
//let cell = UICollectionViewCell() //collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
return cell
看起来在您的 switch 语句中您正在使用 Collectionview 的 indexPath。但是您不想根据所选菜单栏的索引路径显示内容吗?
但是,我会采用一种完全不同的方法,并为另一个视图创建一个 SECOND 集合视图。
【讨论】:
嘿。这是一个带有两个水平排列的自定义单元格的集合视图。每个单元格与屏幕一样宽。然后每个单元格都有一个collectionview(所以它基本上是另一个collectionview的单元格内的collectionviews)。我想为第二个单元重用相同的集合视图。您的意思是我应该创建一个名为“DragonExtra”的单独自定义集合视图单元格吗?只需将我在 DragonCell 中使用的代码复制并粘贴到 DragonCellExtra 中?如果是这样,我已经试过了 @caa5042 如果你这样做,那会让你的生活变得更轻松。你采取的方法有点复杂。但是,您的问题看起来像是使用 collectionview 的 indexPath 的 switch 语句。另一个单元格未建立。通过在集合视图中嵌套集合视图.. 这种不好的做法可能会导致问题。以及无法访问正确的集合视图 是的,我使用了这种方法(一个类用于dragon,另一个用于dragonextra)但完全相同的问题。所以在另一个中嵌套一个collectionview是不好的做法?实际上这是嵌套的三个集合视图。所以我假设情况更糟?它是两个collectionview 单元格,然后每个单元格都有一个collectionview(您看到的三个水平方形单元格)和一个我使用“viewForSupplementaryElementOfKind”函数创建的标题......标题又具有另一个collectionview(轮播)。我有一个解决方法的想法 @caa5042 是的,非常糟糕的做法!您需要在集合视图中创建两个单独的集合视图(或者如果您愿意,您可以使用一个并拥有两个静态单元格)使用 UIView 作为内容视图。在内容视图内部是您拥有所有其他标签和按钮等的地方。我会尽快发布编辑后的答案以上是关于UICollectionView 两个具有相同视图的单元格。第二个单元格不显示任何内容的主要内容,如果未能解决你的问题,请参考以下文章
UICollectionView 中的两个 UITableView 使用相同的引用?