【发布时间】: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