0
votes

how to clean UITableViewCell before reuse ? In cellForRow table view I set up viewModel and in didSet this View Model i change collectionView height constraint but when I scroll very fast then collection is totaly destroyed.

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
    }
}

collection View setup

viewModel Setup:

   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()
        }
    } 

i have also in console 2018-09-19 14:27:52.335285+0200 App[76102:8433084] The behavior of the UICollectionViewFlowLayout is not defined because: 2018-09-19 14:27:52.335409+0200 App[76102:8433084] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

1
show the code you tried it is not useful to give answers - Wings
@V_rohit i added - user8938042

1 Answers

0
votes

If you have a custom tableviewcell class for cells of the tableview. Then there is a method called prepareForReuse(). Use that method to cleanup a cell and prepare it for the new layout.