swift - tableview单元格横向collectionview滚动问题
问题描述
我有一个带有自定义单元格的表格视图,每个单元格都有一个水平的 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())
}
}
我没有得到我所期望的我认为这是因为细胞的重用
解决方案
这通常发生在 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 模式下运行,您可能只需要使用此解决方案,因此您可以在使用此自定义布局之前进行检查
推荐阅读
- ios - ViewController 不能使用通过 sender 参数传递的字符串
- linux - 从 Windows 到 Linux 的 C++ 可移植性
- javascript - node-config Config 在 typescript jest 上下文中是“未定义”吗?
- mysql - Django ORM 获取每个组的最新数据
- c# - 定位程序集的清单定义与程序集引用不匹配
- amazon-cognito - 如何在 AWS Cognito 身份验证过程中包含 TOTP MFA
- python - 如何获得完美的音频时序
- firebase-authentication - 使用 Firebase 身份验证保护 NextJS 中的路由
- sqlite - 谁使用 Dismissible 从数据库中删除任务,使用颤振和 sqlite?
- bash - 在 bash 中排名时处理平局