【问题标题】:Double direction scrolling collectionView with multiple sections data issue具有多个部分数据问题的双向滚动collectionView
【发布时间】:2019-03-15 06:05:29
【问题描述】:

我有一个双向滚动 collectionView,它有 50 个部分,每部分 10 个项目,每部分 3 个显示单元格,并用相同的数据数组填充 collectionView。 问题是每个部分中的 collectionView 重复数据,我想在出现的部分中向用户显示不同的数据。 请提供任何帮助! 这是我的代码:

 func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 50
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: contentCellIdentifier,
                                                  for: indexPath) as! UserCollectionCell

    cell.userImageView.kf.setImage(with: URL.init(string:  String().loadCorrectUrl(url:firebaseUsers[indexPath.row].image))) 
    cell.userNameLabel.text=firebaseUsers[indexPath.row].name
    if(selectedIndexPaths.contains(indexPath) ){
        cell.userBackgroudView.isHidden=false
    }
    else{
        cell.userBackgroudView.isHidden=true
    }
    }

    return cell
}

【问题讨论】:

  • 如何在没有另一个 collectionview 的情况下实现双向滚动

标签: ios swift uicollectionview


【解决方案1】:

UserCollectionCell中实现prepareForReuse,并清除imageView和label文本,防止滚动显示重复数据

class UserCollectionCell: UICollectionViewCell {
    override func prepareForReuse() {
        super.prepareForReuse()
        self.userImageView.image = nil
        self.userNameLabel.text = nil
    }
}

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2017-03-02
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2021-02-08
    • 1970-01-01
    相关资源
    最近更新 更多