【发布时间】: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