一个视图控制器上的 3 个集合视图
3 Collection Views on One View Controller
我在 1 个视图控制器上有 3 个集合视图。我已经尝试了一些在 Stack 上找到的其他建议,但似乎没有任何效果。
所有 3 个集合视图都位于 HomeTableViewController 的单独单元格中。我尝试创建与 HomeTableViewController 的出口连接,但出现错误 Outlets cannot be connected to repeating content
.
我读过很多人能够连接他们的多个 collectionView,所以我对哪里出错有点困惑...
UICollectionView
个实例无法连接到单独 UITableViewController
中的 IBOutlet
个属性。
正如您所描述的,UICollectionView
实际上每个 children 都是它们自己的 parent UITableViewCell
,因此它们不是 [=14] 的直接后代=].这是因为单元格将在 运行 时添加到 UITableView
。
如果您打算在 HomeTableViewController
中创建网点,我建议您像这样创建它们:
private weak var collectionViewA: UICollectionView?
并像这样重写 cellForRow
:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)
// cast cell as collection view parent
collectionViewA = cell.collectionView
return cell
}
如前所述,更好的解决方案是从它们自己的 UITableViewCell
parent 中管理 UICollectionView
个实例。示例:
final class CollectionViewAParentTableViewCell: UITableViewCell {
@IBOutlet private weak var collectionView: UICollectionView!
}
extension CollectionViewAParentTableViewCell: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
…
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
…
}
}
您应该在 UITableViewCell 中创建插座。然后,您可以在 tableView:cellForRowAtIndexPath 方法中为每个单元格中的 collectionViews 提供标签:
yourCell.collectionViewOutlet.tag = indexPath.row + 1000
如果标签与其他视图的标签冲突,您应该将 1000 替换为 Any Constant Integer。
然后使用这些标签来区分collectionview:cellForItemForIndexpath方法中的所有collectionviews:
if(collectionView.tag == 1000){
//Code for collection view in first row of the table
}
else if(collectionView.tag == 1001){
//Code for collection view in second row of the table
}
else if(collectionView.tag == 1002){
//Code for collection view in third row of the table
}
您还应该记住 return 每个集合视图的项目数量,就像上面一样。
标签让生活变得更轻松,不是吗?
快乐编码 (Y)
您应该在 UITableViewCell 中创建插座。然后,您可以在 tableView:cellForRowAtIndexPath 方法中为每个单元格中的 collectionViews 提供标签:
yourCell.collectionViewOutlet.tag = indexPath.row + 1000
如果标签与其他视图的标签冲突,您应该将 1000 替换为 Any Constant Integer。
然后使用这些标签来区分collectionview:cellForItemForIndexpath方法中的所有collectionviews:
if(collectionView.tag == 1000){
//Code for collection view in first row of the table
}
else if(collectionView.tag == 1001){
//Code for collection view in second row of the table
}
else if(collectionView.tag == 1002){
//Code for collection view in third row of the table
}
您还应该记住 return 每个集合视图 collectionView:numberOfItemsInSection 中的项目数,就像上面一样。
标签让生活变得更轻松,不是吗?
快乐编码 (Y)
用不同的集合视图单元格创建三个不同的集合视图,然后你只需要像下面这样在数据源方法中添加:-
if collectionView == collectionViewA{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellA", for: indexPath) as! collectionCell
return cell
}else if collectionView == collectionViewB{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellB", for: indexPath) as! collectionCell
return cell
}else if collectionView == collectionViewC{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellC", for: indexPath) as! collectionCell
return cell
}else{
return UICOllectionViewCell()
}
也对其他数据源方法执行相同的操作。
我在 1 个视图控制器上有 3 个集合视图。我已经尝试了一些在 Stack 上找到的其他建议,但似乎没有任何效果。
所有 3 个集合视图都位于 HomeTableViewController 的单独单元格中。我尝试创建与 HomeTableViewController 的出口连接,但出现错误 Outlets cannot be connected to repeating content
.
我读过很多人能够连接他们的多个 collectionView,所以我对哪里出错有点困惑...
UICollectionView
个实例无法连接到单独 UITableViewController
中的 IBOutlet
个属性。
正如您所描述的,UICollectionView
实际上每个 children 都是它们自己的 parent UITableViewCell
,因此它们不是 [=14] 的直接后代=].这是因为单元格将在 运行 时添加到 UITableView
。
如果您打算在 HomeTableViewController
中创建网点,我建议您像这样创建它们:
private weak var collectionViewA: UICollectionView?
并像这样重写 cellForRow
:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)
// cast cell as collection view parent
collectionViewA = cell.collectionView
return cell
}
如前所述,更好的解决方案是从它们自己的 UITableViewCell
parent 中管理 UICollectionView
个实例。示例:
final class CollectionViewAParentTableViewCell: UITableViewCell {
@IBOutlet private weak var collectionView: UICollectionView!
}
extension CollectionViewAParentTableViewCell: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
…
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
…
}
}
您应该在 UITableViewCell 中创建插座。然后,您可以在 tableView:cellForRowAtIndexPath 方法中为每个单元格中的 collectionViews 提供标签:
yourCell.collectionViewOutlet.tag = indexPath.row + 1000
如果标签与其他视图的标签冲突,您应该将 1000 替换为 Any Constant Integer。
然后使用这些标签来区分collectionview:cellForItemForIndexpath方法中的所有collectionviews:
if(collectionView.tag == 1000){
//Code for collection view in first row of the table
}
else if(collectionView.tag == 1001){
//Code for collection view in second row of the table
}
else if(collectionView.tag == 1002){
//Code for collection view in third row of the table
}
您还应该记住 return 每个集合视图的项目数量,就像上面一样。
标签让生活变得更轻松,不是吗? 快乐编码 (Y)
您应该在 UITableViewCell 中创建插座。然后,您可以在 tableView:cellForRowAtIndexPath 方法中为每个单元格中的 collectionViews 提供标签:
yourCell.collectionViewOutlet.tag = indexPath.row + 1000
如果标签与其他视图的标签冲突,您应该将 1000 替换为 Any Constant Integer。
然后使用这些标签来区分collectionview:cellForItemForIndexpath方法中的所有collectionviews:
if(collectionView.tag == 1000){
//Code for collection view in first row of the table
}
else if(collectionView.tag == 1001){
//Code for collection view in second row of the table
}
else if(collectionView.tag == 1002){
//Code for collection view in third row of the table
}
您还应该记住 return 每个集合视图 collectionView:numberOfItemsInSection 中的项目数,就像上面一样。
标签让生活变得更轻松,不是吗? 快乐编码 (Y)
用不同的集合视图单元格创建三个不同的集合视图,然后你只需要像下面这样在数据源方法中添加:-
if collectionView == collectionViewA{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellA", for: indexPath) as! collectionCell
return cell
}else if collectionView == collectionViewB{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellB", for: indexPath) as! collectionCell
return cell
}else if collectionView == collectionViewC{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellC", for: indexPath) as! collectionCell
return cell
}else{
return UICOllectionViewCell()
}
也对其他数据源方法执行相同的操作。