【问题标题】:Collectionview Reloaddata function not workingCollectionview Reloaddata 功能不起作用
【发布时间】:2019-07-04 22:36:47
【问题描述】:

我有一个 collectionView,我在其中显示带有 UIButton 的普通 UICollectionViewCell。在另一个屏幕上,我有一个开关列表。每当这些更改时,就会使用 collectionView 将更新发送到屏幕。当我关闭开关(并且数据源已更新)时,我尝试使用 reloadData 函数重新填充 collectionView。但是,按钮(单元格)显示在彼此的顶部。

我尝试使用 reloadData,在 Dispatch.main.async 中执行此操作,使 collectionView 无效,删除 deleteSections 函数并访问特定单元格并使用 cell.removeFromSuperView() 将其删除。

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath)
        let name = PickingState.getSelectedFilterValues()[indexPath.row]
        let button = SelectionsItem(title: name, frame: createMockupButtonToGetFrame(name: name))

        print(name)

        cell.contentView.addSubview(button)
        cell.contentView.addConstraintsWithFormat(formatString: "H:|[v0]|", views: button)
        cell.contentView.addConstraintsWithFormat(formatString: "V:|[v0]|", views: button)

        return cell
    }

期望单元格重新渲染,而不是彼此重叠。

【问题讨论】:

    标签: swift uicollectionview reloaddata


    【解决方案1】:

    问题来了

    cell.contentView.addSubview(button)
    

    由于单元格出列,您需要在添加之前清除所有按钮

    cell.contentView.forEach { 
      if $0.tag == 22 {
         $0.removeFromSuperview()
      }
    }
    let button = SelectionsItem(title: name, frame: createMockupButtonToGetFrame(name: name))
    button.tag = 22
    cell.contentView.addSubview(button)
    

    别忘了

    button.translatesAutoresizingMaskIntoConstraints = false
    

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多