【问题标题】:how to reference a UICollectionView when its inside a UITableViewCell如何在 UITableViewCell 内引用 UICollectionView
【发布时间】:2023-04-02 12:07:01
【问题描述】:

我有一个 UITableView,它有 2 个部分。第 1 部分是一个静态单元格,里面有一个水平的 collectionView。

我的问题是如何在Controller中引用collectionView来重新加载collectionView...

这是我的代码:

TableView 控制器

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "collectionCellID", for: indexPath) as! CollectionTableViewCell

        return cell

    } else {

        let cell = tableView.dequeueReusableCell(withIdentifier: "tableCellID", for: indexPath) as! TableCell

        return cell
    }
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == 0 {
        if let cell = cell as? CollectionTableViewCell {
            cell.collectionView.delegate = self
            cell.collectionView.dataSource = self
            cell.collectionView.contentInset = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
        }
    }
}

表格视图单元格

class CollectionTableViewCell: UITableViewCell {

@IBOutlet weak var collectionView: UICollectionView!

}

CollectionView 扩展

extension MyController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    return cell
}

TableViewController 中的数据调用

public func getData() {

    ref.observe(.childAdded, with: { (snapshot) in

                self.data.append(snapshot)
            }

            DispatchQueue.main.async {
                //MARK: - collectionView.reloadData() <- not available
            }
        }
    })
}

【问题讨论】:

    标签: ios uitableview model-view-controller uicollectionview reloaddata


    【解决方案1】:

    调用表格视图的 cellForRow(at:) 以获取对第 0 行第 0 行的单元格的引用,将该引用转换为 CollectionTableViewCell,并引用其 collectionView。

    【讨论】:

      【解决方案2】:

      我很难配置相同的问题 (https://stackoverflow.com/a/45618501/3400991)。以下是关于此的几点:

      1. 您的控制器应符合 UICollectionViewDataSource、UICollectionViewDelegate、UICollectionViewDelegateFlowLayout
      2. 创建 CollectionView 单元并将其自定义类设置为 CollectionView 单元。
      3. Tableview 不知道它的单元格需要多少高度才能在 tableview 单元格内呈现完整的 collectionview 数据,所以你必须使用这个:

        yourTableView.rowHeight = UITableViewAutomaticDimension yourTableView.estimatedRowHeight = 90

      4. 相应地设置 Tableview 的高度:

      // 因为 Collectionview 明确告诉它的 Parent 提供高度

      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
      
          //Checking MIME Type
          if condition for collectionview cell {
              // set here 
          }
          //Normal autolayout cell
          else {
              return UITableViewAutomaticDimension
          }
      }
      
      1. 在 Tableview 自定义单元类中创建 Collectionview 引用:

      类 customTableViewCell: UITableViewCell { @IBOutlet 弱 var 集合视图:UICollectionView }

      1. Tableview 内部 willDisplayCell :

        func tableView(_tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

                cell.collectionview.delegate = self
                cell.collectionview.datasource = self
                //Reload it also
                cell.collectionview.reloadData()
        }
        

      【讨论】:

      • 我不能在 willDisplayCell 中使用 cell.collectionView.reloadData(),因为每次用户上下滚动时,单元格都会继续显示。导致 collectionView 被不必要地重新加载
      • 否则,您应该使用collectionview,并与collectinoviw一起使用作为tableview
      • 那么我会遇到相反的问题。如果我要遵循 MVC,我将无法访问我的 tableView 以重新加载它...这与我在 collectionView 中遇到的问题相同...
      【解决方案3】:

      所以我最终做的是为表格创建一个 Header View 并向其添加一个 collectionView。

      func configureHeaderView() {
      
          let layout = UICollectionViewFlowLayout()
          layout.scrollDirection = .horizontal
          layout.sectionInset = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
      
          collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 105), collectionViewLayout: layout)
          collectionView.backgroundColor = UIColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1)
          collectionView.isPagingEnabled = false
          collectionView.isUserInteractionEnabled = true
          collectionView.dataSource = self
          collectionView.delegate = self
          collectionView.register(UINib(nibName: "cell", bundle: nil), forCellWithReuseIdentifier: "cell")
          collectionView.showsHorizontalScrollIndicator = false
      
          tableView.tableHeaderView = collectionView
      }
      

      然后从我现在可以访问的任何地方:

      DispatchQueue.main.async {
                     self?.collectionView.reloadData()
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多