gpt4 book ai didi

ios - UI 中显示的单元格数量小于数据计数

转载 作者:行者123 更新时间:2023-11-29 05:28:01 25 4
gpt4 key购买 nike

猜猜我的 cellForItemAt 的 collectionView 函数没有正确调用。第二行和第三行计数丢失。即行计数的输出为
行=> 0
行 => 1
行=> 4
行=> 5
但我的数据计数是 6,这是正确的。否则,显示的 4 个单元格与图像和其他数据完美匹配。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

cell = collectionView.dequeueReusableCell(withReuseIdentifier: CELL_IDENTIFIER, for: indexPath) as? CollectionViewCell

var row = indexPath.row
print ("row =>", row)
let imageView = UIImageView()
var imgURL: String? = nil
if let object = (dataSource[row] as? [AnyHashable : Any])?["src"] {
imgURL = "\(object)"
}
let url = URL(string: imgURL ?? "")
cell?.cellImageView.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder.png"), completed: {
[weak self] (image, error, cacheType, imageURL)
in
})
imageView.contentMode = .scaleAspectFill
var shoptitle: String? = nil
if let object = (dataSource[row] as? [AnyHashable : Any])?["shop_name"] {
shoptitle = "\(object)"
}
cell?.txtDescription.text = shoptitle
cell?.shareIcon.tag = indexPath.item
cell?.cellImageView.tag = indexPath.item
cell?.cellImageView.isUserInteractionEnabled = true
cell?.shareIcon.addTarget(self, action: #selector(click(_:)), for: .touchUpInside)
cell?.layer.shadowOpacity = 0.5
cell?.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
cell?.layer.shadowRadius = 10.0
cell?.layer.shadowColor = UIColor.gray.cgColor
return cell!
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

print(String(format: "Data Count :%lu", UInt(dataSource.count)))
return dataSource.count
}

最佳答案

不要将cell存储为实例变量。

替换

cell = collectionView.dequeueReusableCell(withReuseIdentifier: CELL_IDENTIFIER, for: indexPath) as? CollectionViewCell

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CELL_IDENTIFIER, for: indexPath) as? CollectionViewCell

编辑:

使用collectionView.cellForItem(at: indexPath)didSelectItemAt方法中获取单元格。

关于ios - UI 中显示的单元格数量小于数据计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57967770/

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