我有一个带有自定义单元格的表格视图,每个单元格都有一个水平collectionView。因此,当重新加载tableView时,我可以正确获取tableview中的数据,因此第一个tableViewCell可以很好地与集合视图一起工作,但其他单元格的滚动不是从开始
我试图在tableView单元格中创建一个collectionview,它工作正常,但是滚动无法按我的需要工作
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())
}
}
我没有得到我所期望的,这是因为单元的重用
答案 0 :(得分:0)
这种情况通常发生在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
中,要确保已加载笔尖,而忽略了它已被重用的事实,您将把collectionView流布局设置为我们在上面创建的布局
override func awakeFromNib() {
super.awakeFromNib()
categories.collectionViewLayout = ArabicCollectionFlow()
}
请注意,必须在您的reloadData
此外,如果您的应用程序以RTL模式运行,则可能仅需要使用此解决方案,因此您可以在使用此自定义布局之前进行检查