【发布时间】:2018-09-20 10:53:02
【问题描述】:
代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
registerCells()
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifierForItem(at: indexPath.row), for: indexPath) as? HorizontalCollectionTableViewCell else {
return UITableViewCell()
}
if cellViewModels.count > indexPath.row {
let viewModel = cellViewModels[indexPath.row]
cell.viewModel = viewModel
}
return cell
}
将viewModel 传递给单元:
var viewModel: TitleAccessoryButtonCollectionViewModel? {
didSet {
guard let viewModel = viewModel else {
return
}
titleLabel.text = viewModel.title
if let buttonTitle = viewModel.accessoryButtonModel?.title {
setAccessoryButtonTitle(buttonTitle)
}else{
accessoryButton.hideTitleLabel()
}
if let buttonImage = viewModel.accessoryButtonModel?.image {
accessoryButton.buttonImageView.image = buttonImage
}
else {
accessoryButton.hideImageView()
}
sectionContentImage.image = viewModel.sectionContentImage
titleLabelLeadingConstraint.constant = viewModel.titleLabelLeadingSpacing
accessoryButton.isHidden = viewModel.hideAccessoryButton
sectionContentView.isHidden = viewModel.hidePremiumContentView
let collectionViewModel = viewModel.collectionViewModel
collectionViewHeight.constant = CGFloat(collectionViewModel.height)
collectionViewModel.setup(collectionView: collectionView)
collectionView.delegate = collectionViewModel.delegate
collectionView.dataSource = collectionViewModel.dataSource
collectionView.reloadData()
}
}
说明:
我主要有六个UITableViewCell,它们是可重复使用的。
在每个UITableViewCell 中都是UICollectionView。
五个UICollectionView's 使用普通的UICollectionViewFlowLayout's,但一个需要自定义子类。
问题是,当 UITableViewCell 和自定义 UICollectionViewFlowLayout 被隐藏并且新 UITableViewCell 正在显示并且具有此自定义流布局的单元格被重用并且 UICollectionView 已经有 UICollectionViewFlowLayout 但很糟糕时。
有什么好方法可以清除这种布局或防止这种情况发生吗?
也许是prepareForReuse()?
我补充说UICollectionView 是UITableViewCell 的出口。
【问题讨论】:
-
你能张贴一张你想要达到的目标的图片吗?
-
自定义 UICollectionViewFlowLayouts 表示 2 行中的项目彼此靠近。正常只是具有良好间距的水平集合,并且此自定义在应该是正常流布局的行中重复使用。就这样
标签: ios swift uitableview uicollectionview