如何在重用之前清理UITableViewCell?在cellForRow表视图中,我设置了viewModel,在didSet中,设置了该视图模型,我更改了collectionView的高度限制,但是当我快速滚动时,集合将被完全破坏。
func setup(collectionView: UICollectionView) {
if self.collectionView != collectionView || self.collectionView == nil {
collectionView.register(UINib(nibName: "HomeRecommendationCollectionViewCell", bundle: nil),
forCellWithReuseIdentifier: HomeStoryboardConsts.Identifier.homeRecommendationCollectionViewCell.rawValue)
collectionView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10)
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.minimumInteritemSpacing = 10
}
disposeBag = DisposeBag()
items.asObservable()
.subscribe(onNext: { [weak collectionView] _ in
collectionView?.reloadData()
})
.addDisposableTo(disposeBag)
self.collectionView = collectionView
}
}
收藏夹视图设置
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()
}
}
我也在控制台中 2018-09-19 14:27:52.335285 + 0200 App [76102:8433084]未定义UICollectionViewFlowLayout的行为是因为: 2018-09-19 14:27:52.335409 + 0200 App [76102:8433084]项的高度必须小于UICollectionView的高度减去该部分插入的顶部和底部值,减去内容插入的顶部和底部值。>
答案 0 :(得分:0)
如果您具有用于表视图单元格的自定义tableviewcell类。然后是一个称为prepareForReuse()
的方法。使用该方法清理单元格,并为新布局做好准备。