gpt4 book ai didi

swift - 嵌套 UICollectionView 重复单元格

转载 作者:行者123 更新时间:2023-11-30 11:56:14 27 4
gpt4 key购买 nike

我有一段时间遇到一个问题,但还没有解决。最后,我想就以下问题寻求您的帮助。

我在我的项目中实现了一个嵌套的UICollectionView,并且在主 Collection View 上向下滚动时出现重复的单元格。

我不知道如何解决这个问题。

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)

}
}

最佳答案

每次显示时都需要重新加载内部 Collection View

   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
}

关于swift - 嵌套 UICollectionView 重复单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47787192/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com