【问题标题】:UICollectionView destroyed in UITableViewCellUICollectionView 在 UITableViewCell 中销毁
【发布时间】:2024-01-19 16:00:01
【问题描述】:

如何在重用之前清理 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 的高度减去部分插入顶部和底部值,减去内容插入顶部和底部值。

【问题讨论】:

  • 显示你试过的代码,给出答案没有用
  • @V_rohit 我添加了

标签: ios swift uitableview uicollectionview


【解决方案1】:

如果你有一个用于 tableview 单元格的自定义 tableviewcell 类。然后有一个方法叫prepareForReuse()。使用该方法清理单元格并为新布局做好准备。

【讨论】:

  • 我尝试将 viewModel 和 collectionView 设为 nil,但由于 nil 而崩溃
  • 你想在哪里清零视图模型?
  • 因为每个单元格都有自己的视图模型,所以我试图 nil 前一个