我有一段时间有问题,但我还是无法解决。最后,我想就下列问题征求您的帮助。
我在项目中实现了嵌套的UICollectionView
,并且在向下滚动主集合视图时我重复了单元格。
我无法弄清楚如何克服这个问题。
MainPageViewController.swift
import UIKit
class MainPageViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
print("icinde")
let navBar = UINavigationBar()
let navItem = UINavigationItem(title: "Promotions")
navBar.setItems([navItem], animated: false)
self.view.addSubview(navBar)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
var collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(MainWithCarouselCollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.white
self.view.addSubview(collectionView)
let views = ["navBar":navBar,"collectionView": collectionView] as [String : Any]
// 2
var allConstraints = [NSLayoutConstraint]()
let navBarHeight = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-0-[navBar]-0-|",
options: [],
metrics: nil,
views: views)
allConstraints += navBarHeight
let navBarWidth = NSLayoutConstraint.constraints(
withVisualFormat: "V:|-20-[navBar]",
options: [],
metrics: nil,
views: views)
allConstraints += navBarWidth
let collectionViewTop = NSLayoutConstraint.constraints(
withVisualFormat: "V:[navBar]-10-[collectionView]-|",
options: [],
metrics: nil,
views: views)
allConstraints += collectionViewTop
let collectionViewleftRight = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-5-[collectionView]-5-|",
options: [],
metrics: nil,
views: views)
allConstraints += collectionViewleftRight
navBar.translatesAutoresizingMaskIntoConstraints=false
collectionView.translatesAutoresizingMaskIntoConstraints=false
NSLayoutConstraint.activate(allConstraints)
// Do any additional setup after loading the view.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MainWithCarouselCollectionViewCell
cell.labelicerik = "\(indexPath.row)"
print(indexPath.row)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:view.frame.width,height: 150)
}
}
MainWithCarouselCollectionViewCell.swift
import UIKit
class MainWithCarouselCollectionViewCell: UICollectionViewCell,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate {
private let cellId = "Cell2"
var labelicerik:String = {
return ""
}()
let appsCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
layout.scrollDirection = .horizontal
return collectionView
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
backgroundColor = .blue
addSubview(appsCollectionView)
appsCollectionView.delegate = self
appsCollectionView.dataSource = self
appsCollectionView.register(AppCell.self, forCellWithReuseIdentifier: cellId)
let views = ["appsCollectionView": appsCollectionView] as [String : Any]
// 2
var allConstraints = [NSLayoutConstraint]()
let collectionViewTop = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-0-[appsCollectionView]-0-|",
options: [],
metrics: nil,
views: views)
allConstraints += collectionViewTop
let collectionViewleftRight = NSLayoutConstraint.constraints(
withVisualFormat: "V:|-0-[appsCollectionView]-0-|",
options: [],
metrics: nil,
views: views)
allConstraints += collectionViewleftRight
appsCollectionView.translatesAutoresizingMaskIntoConstraints=false
NSLayoutConstraint.activate(allConstraints)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 15
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! AppCell
cell.testlabel.text = "\(labelicerik) + \(indexPath.row) "
print(indexPath.row)
print(labelicerik)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:contentView.frame.width/2,height: contentView.frame.height)
}
}
class AppCell: UICollectionViewCell {
let testlabel: UILabel = {
let testlabel = UILabel(frame: .zero)
return testlabel
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews(){
backgroundColor = .red
addSubview(testlabel)
testlabel.textAlignment = .center
let views = ["testlabel": testlabel] as [String : Any]
// 2
var allConstraints = [NSLayoutConstraint]()
let collectionViewTop = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-0-[testlabel]-0-|",
options: [],
metrics: nil,
views: views)
allConstraints += collectionViewTop
let collectionViewleftRight = NSLayoutConstraint.constraints(
withVisualFormat: "V:|-0-[testlabel]-0-|",
options: [],
metrics: nil,
views: views)
allConstraints += collectionViewleftRight
testlabel.translatesAutoresizingMaskIntoConstraints=false
NSLayoutConstraint.activate(allConstraints)
}
}
答案 0 :(得分:0)
每次显示时都需要重新加载内部集合视图
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MainWithCarouselCollectionViewCell
cell.labelicerik = "\(indexPath.row)"
// you need to call this
cell.appsCollectionView.reloadData
return cell
}