【发布时间】:2018-03-09 13:23:22
【问题描述】:
我有一个UITableView,有 2 个部分,每个部分包含一个单元格。这些单元格包含UICollectionViews。这些集合视图具有不同的类型。为了遵循 MVC 设计模式,我将我的 ViewController 设置为数据源和 UITableView 和 UICollectionView 的委托。
这是我的表格视图的数据源代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: recomendationCellId, for: indexPath) as! RecomendationsTableViewCell
cell.collectionView.dataSource = self
cell.collectionView.delegate = self
return cell
} else if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: collectionCellId, for: indexPath) as! CollectionsTableViewCell
cell.collectionView.dataSource = self
cell.collectionView.delegate = self
return cell
} else {
return UITableViewCell()
}
}
现在,我需要使用collectionView(_:,cellForItem:) 方法将集合视图的单元格出列。在其中我需要检查表格视图的部分以使正确的单元格出列。
问题是我知道它应该有一个简单的解决方案,但无法弄清楚。您对如何实现这一点有什么建议吗?
【问题讨论】:
-
您可以继承 collectionView 并添加一个属性
并在您将 tableViewCell 出列时设置它
标签: ios swift tableview collectionview