【问题标题】:The problem of scrolling of horizontal collectionview in tableview celltableview单元格横向collectionview滚动问题
【发布时间】:2020-02-14 05:19:26
【问题描述】:

我有一个带有自定义单元格的表格视图,每个单元格都有一个水平的 collectionView。因此,当重新加载 tableView 时,我可以正确获取 tableview 中的数据,因此第一个 tableViewCell 与集合视图配合良好,但其他单元格的滚动不是从头开始

我尝试在 tableView 单元格中创建一个集合视图,它工作正常,但滚动无法按我的需要工作

    extension HomeVC:UITableViewDataSource,UITableViewDelegate
{
    func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return (self.viewModel.homee?.data?.banners?.count ?? 0) + 1
    }



    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return HelpingMethods.setProductHeight() + 50
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueCellForTable(ofType: ProductSliderCell.self)

                cell.configureCell(banner.content, nil,0)
                return cell

    }
}

对于集合视图,我使用此代码

class ProductSliderCell: UITableViewCell {



override func awakeFromNib() {
    super.awakeFromNib()
    collectionView.registerNibForCollection(ofType: productItem.self)
}

@IBOutlet weak var collectionView: UICollectionView!
override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

    extension ProductSliderCell:UICollectionViewDelegate,UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout
{

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

            return  self.contentOffers?.products?.count ?? 0


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

        let cell = collectionView.dequeueCellForCollection(ofType: productItem.self, withIndex: indexPath)
        if let product = self.content?.products?[indexPath.item] {
                cell.configureCellForHome(product)

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        cell.layoutIfNeeded()
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.size.width/2.8, height:  HelpingMethods.setProductHeight())
    }
}

我没有得到我所期望的我认为这是因为细胞的重用

【问题讨论】:

  • 您的预期行为是什么?它现在的表现如何?也许您需要为您的 collectionView 明确指定它们将仅水平滚动?
  • 在第一个单元格中,滚动从第一项开始,在其他单元格中,它不从第一项开始
  • the scroll begin 是什么意思?您可以添加一个包含正确单元格和错误单元格的屏幕截图吗?你是说边距吗?还是什么?
  • 让我解释一下,如果我有 10 个表格视图单元格,每个表格视图单元格都有一个包含 10 个项目的水平集合视图,当我运行项目时,第一个表格视图单元格从第一个项目开始集合视图,其他集合视图不开始从一开始就清楚了吗?
  • 所以你是说第一个collectionView单元格的contentOffset是0,但是下一个单元格的内容偏移量是> 0?如果是这种情况,请尝试将 collectionView.contentOffset = 0 添加到您的 numberOfItemsInsection 方法中。

标签: swift uitableview uicollectionview


【解决方案1】:

这通常发生在 RTL 方向的应用程序中,当单元格被重用时,它不会消除它在它之前的单元格中的位置

解决办法是总是强制 UICollectionView 被翻转,每次加载单元格时,使用 collectionView 的流布局属性flipsHorizontallyInOppositeLayoutDirection 来返回 true,

1) 创建具有以下属性的自定义流布局类:

class ArabicCollectionFlow: UICollectionViewFlowLayout {
    override func prepare() {
        super.prepare()
        self.scrollDirection = .horizontal
    }
    override var flipsHorizontallyInOppositeLayoutDirection: Bool {
        return true
    }
}

在您的 Tableviewcell 类中,在 awakeFromNib 中确保 nib 已加载,忽略它被重用的事实,您将设置 collectionView 流布局为我们在上面创建的布局

override func awakeFromNib() {
    super.awakeFromNib()
    categories.collectionViewLayout = ArabicCollectionFlow()
}

请注意,此流程布局必须在您的 reloadData 之后设置

此外,如果您的应用在 RTL 模式下运行,您可能只需要使用此解决方案,因此您可以在使用此自定义布局之前进行检查

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    相关资源
    最近更新 更多