【问题标题】:Cells are not populating in second collection view inside of table单元格未填充在表格内的第二个集合视图中
【发布时间】:2020-09-28 22:44:11
【问题描述】:

我是编码新手,我对当前的问题失去了理智。我有一个包含 7 个单元的表格视图控制器。我在 7 个单元格中有 4 个集合视图。我在其他三个有标签。我试图让我的前两个集合视图工作,这样我就可以攻击相同的方法来添加另外两个。我目前只能填充我的第一个集合视图(在单元格 2 中)的单元格。我不知道如何填充第二个集合视图的单元格。

这是表格视图控制器的代码:

import UIKit

class HomeTableViewController: UITableViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var t10MacInfCollectionView: UICollectionView!

@IBOutlet weak var t10MicroInfCollectionView: UICollectionView!

let T10Mac = "T10Mac"

let T10Mic = "T10Mic"

override func viewDidLoad() {
    super.viewDidLoad()
    
    t10MacInfCollectionView.delegate = self
    t10MicroInfCollectionView.delegate = self

    t10MacInfCollectionView.dataSource = self
    t10MicroInfCollectionView.dataSource = self

    self.view.addSubview(t10MicroInfCollectionView)
    self.view.addSubview(t10MicroInfCollectionView)

    t10MacInfCollectionView.reloadData()
    t10MicroInfCollectionView.reloadData()

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    if collectionView == self.t10MacInfCollectionView {
        
        return 10
        
        }

        return 10
    }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    if collectionView == self.t10MacInfCollectionView {
        
        let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: T10Mac , for: indexPath) as! T10MacroCollectionViewCell
        
        return cellA
        
        }

    else {

        let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: T10Mic , for: indexPath) as! T10MicroCollectionViewCell

        return cellB
            
        }

    }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
    if collectionView == self.t10MacInfCollectionView {
        
        return CGSize(width: 125.0, height: 225.0)
        
           }
    
    else {

            return CGSize(width: 125.0, height: 225.0)
        
               }
    }

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 7
    }
}

我真的很沮丧。如果有人可以请帮助,我将非常感激。谢谢大家的帮助。

【问题讨论】:

  • 快速浏览一下,我立即看到一个似乎有很大后果的小错字。在将 collectionViews 添加为子视图时,您将相同的添加两次,而不是每次添加一次。
  • 爬行者说话!非常感谢。我改变了它,它最终删除了我的一些单元格。我最终删除了 addSubview 并解决了我的问题。感谢您帮助我解决问题!

标签: swift xcode uitableview uicollectionview multiple-instances


【解决方案1】:

在 viewDidLoad() 函数中:删除

self.view.addSubview(t10MicroInfCollectionView)
self.view.addSubview(t10MicroInfCollectionView)

viewDid 现在应该如下所示:

override func viewDidLoad() {
super.viewDidLoad()

t10MacInfCollectionView.delegate = self
t10MicroInfCollectionView.delegate = self

t10MacInfCollectionView.dataSource = self
t10MicroInfCollectionView.dataSource = self

t10MacInfCollectionView.reloadData()
t10MicroInfCollectionView.reloadData()

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多