Code:
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
}
Passing viewModel
to Cell:
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()
}
}
Description:
I have six UITableViewCell
mostly, and they are reusable.
In every UITableViewCell
is UICollectionView
.
Five UICollectionView's
use normal UICollectionViewFlowLayout's
, but one needs a custom subclass.
The problem is that when UITableViewCell
with custom UICollectionViewFlowLayout
is hiding and new UITableViewCell is showing and cell with this custom flow layout is reused and UICollectionView
already have UICollectionViewFlowLayout
but is bad.
Is any nice way to clear this layout or prevent this situation?
Maybe something with prepareForReuse()
?
I add that UICollectionView
is outlet in UITableViewCell
.
This excellent article helped me a lot to get UICollectionViews in UITableviewCells up and running: https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
To update the layout you can call
collectionView.collectionViewLayout.invalidateLayout()
See also: Swift: How to refresh UICollectionView layout after rotation of the device
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.