gpt4 book ai didi

swift - dequeueReusableCell 在 3 个单元格后不出队

转载 作者:行者123 更新时间:2023-11-28 15:24:57 24 4
gpt4 key购买 nike

我在另一个 uicollectionview 中有一个 uicollectionview。当我水平滚动单元格时,如您所见,“顶部单元格”(包含其他 collectionview 的单元格)没有出队:

enter image description here

如您所见,“Randonnées”与“Aborigènes”相同。

我已经尽我所能尝试了 prepareForReuse(),但它似乎不起作用。我知道其他人遇到了一个非常相似的问题,但没有一个相关主题对我有用:/

这是我的一些代码:

class X3ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

// MARK: Variables
let cellID = "pouet"
let delegate = UIApplication.shared.delegate as! AppDelegate
var cell : X3ContentCollectionViewCell?

// MARK: UI
let titleView : UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

var mainCollectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
view.collectionViewLayout = layout
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
view.isPagingEnabled = true
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .white
return view
}()

var menuBar: X3MenuBar?

override func viewWillAppear(_ animated: Bool) {
print(delegate.ds.X3data)
}

// MARK: ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blue
mainCollectionView.dataSource = self
mainCollectionView.delegate = self
mainCollectionView.register(X3ContentCollectionViewCell.self, forCellWithReuseIdentifier: cellID)
menuBar = X3MenuBar()
menuBar?.view.translatesAutoresizingMaskIntoConstraints = false
menuBar?.rootController = self
addMenuScrollView()
addMainView()

}

// MARK: Methods
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return delegate.ds.X3data!.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell = mainCollectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as? X3ContentCollectionViewCell
cell?.indexOfContent = indexPath.row
return cell!
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height-25)
}

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let screenWidth = UIScreen.main.bounds.size.width
let itemIndex = Int(targetContentOffset.pointee.x / screenWidth)
menuBar?.scrollToItemIndex(item: itemIndex)

}

func scrollToItemIndex(item: Int) {
mainCollectionView.selectItem(at: IndexPath(row: item, section: 0), animated: true, scrollPosition: .centeredHorizontally)
delegate.ds.X3index = item
}



//MARK: UI Methods


func addMainView() {
view.addSubview(mainCollectionView)
mainCollectionView.topAnchor.constraint(equalTo: (menuBar?.view.bottomAnchor)!).isActive = true
mainCollectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
mainCollectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
mainCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}

func addTitleView() {
view.addSubview(titleView)
titleView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
titleView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
titleView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
titleView.heightAnchor.constraint(equalToConstant: delegate.gfx.X1ViewTitleHeigth!).isActive = true
}

func addMenuScrollView() {
view.addSubview((menuBar?.view)!)
menuBar?.view.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
menuBar?.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
menuBar?.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
menuBar?.view.heightAnchor.constraint(equalToConstant: 30).isActive = true

}

deinit {
delegate.ds.X3data = nil
}

}

还有另一个重要的部分:

class X3ContentCollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

let cellId = "pouet"
let delegate = UIApplication.shared.delegate as! AppDelegate
var indexOfContent = 0

var customCollectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.collectionViewLayout = layout

cv.backgroundColor = .white
cv.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
cv.translatesAutoresizingMaskIntoConstraints = false
return cv

}()

override init(frame: CGRect) {
super.init(frame: frame)
customCollectionView.register(X3ContentMenuCellCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
customCollectionView.dataSource = self
customCollectionView.delegate = self
addSubview(customCollectionView)
customCollectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true
customCollectionView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
customCollectionView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
customCollectionView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5).isActive = true
customCollectionView.reloadData()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return ((delegate.ds.X3data?[self.indexOfContent] as! NSDictionary)["images"] as! NSArray).count
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.item == 0 {
return CGSize(width: frame.size.width, height: 170)
} else if indexPath.item == 3 {
return CGSize(width: frame.size.width, height: 170)
}else {
return CGSize(width: frame.size.width/2, height: 120)
}
}

override func prepareForReuse() {

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = customCollectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! X3ContentMenuCellCollectionViewCell
print((((delegate.ds.X3data?[self.indexOfContent] as! NSDictionary)["images"] as! NSArray)[indexPath.row] as! NSDictionary)["image"])
cell.X3Label.text = "\((((delegate.ds.X3data![self.indexOfContent] as! NSDictionary)["images"] as! NSArray)[indexPath.row] as! NSDictionary)["image"]!) \(self.indexOfContent)"
cell.X3ImageView.image = UIImage(named: (((delegate.ds.X3data?[self.indexOfContent] as! NSDictionary)["images"] as! NSArray)[indexPath.row] as! NSDictionary)["image"] as! String)
cell.X3ImageView.contentMode = .scaleAspectFill
cell.X3ImageView.clipsToBounds = true

if indexPath.item == 0 {
cell.X3Label.font = delegate.gfx.font(style: 2)
} else if indexPath.item == 3 {
cell.X3Label.font = delegate.gfx.font(style: 2)
}else {
cell.X3Label.font = delegate.gfx.font(style: 4)
}
return cell
}



deinit {
}
}

最佳答案

mrabins 是对的。试试这个,看看它是否有效。似乎当设置了 indexOfContent 时,您的数据就准备好了。所以只需重新加载即可。

var indexOfContent = 0 {
didSet {
customCollectionView.reloadData()
}
}

关于swift - dequeueReusableCell 在 3 个单元格后不出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45401350/

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