gpt4 book ai didi

swift - tableview cell中水平collectionview的滚动问题

转载 作者:行者123 更新时间:2023-11-30 10:34:11 57 4
gpt4 key购买 nike

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

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

    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

}
}

对于 Collection View ,我使用此代码

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())
}
}

我没有得到我期望的结果,我认为这是因为单元格的重用

最佳答案

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

解决方案是每次加载单元格时始终强制 UICollectionView 翻转,使用 collectionView 的流布局属性 flipsHorizo​​ntallyInOppositeLayoutDirection 返回 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 模式下运行,您可能只需要使用此解决方案,因此您可以在使用此自定义布局之前进行检查

关于swift - tableview cell中水平collectionview的滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58430031/

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