【问题标题】:I have collectionView inside of Tableview cell. It return always Zero. How can I solve it?我在 Tableview 单元格中有collectionView。它总是返回零。我该如何解决?
【发布时间】:2020-04-29 15:42:07
【问题描述】:

我在 Tableview 单元格中有 collectionView。当我从 firebase 获取数据时,我将数据保存到数组中,然后尝试将其放在集合视图中。但是,即使我手动将数据放入数组中,我的 array.count 始终显示为零并且我的集合视图中没有任何内容。

override func viewDidLoad() {
    super.viewDidLoad()
    currentuserArray.append(User(email: "x@gmail.com", image: "ASD"))
    currentuserArray.append(User(email: "y@gmail.com", image: "ASD"))
    print(currentuserArray.count)
    tableView.delegate = self
    tableView.dataSource = self
    getDataFromFirebaseToTakeUsers()

}

其中 currentuserArray.count 的值为 2。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(currentuserArray.count)
        return currentuserArray.count // this looks like 0, however at the top, it was 2.
    }

但是;在那里,userMailArray.count 的值为 0。所以,我的 collectionView 上没有任何内容。怎么可能?

【问题讨论】:

  • 是集合还是表格?
  • 您没有遵循视图控制器的生命周期。为了填充您的数组,您必须在将数据提取到数组后在 getDataFromFirebaseToTakeUsers() 中调用“collectionview.reloadData”。如果你不这样做,你的数组将永远是空的。因为collectionview方法在你的viewdidload方法之前调用......希望你理解...... :)

标签: ios swift uicollectionview swift4 mobile-development


【解决方案1】:

问题是getDataFromFirebaseToTakeUsers是异步的,你需要

self.collectionView.reloadData()

里面的补全

【讨论】:

  • func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "reviewCell") as! ReviewTableViewCell cell.clCollectionView.reloadData() 返回单元格 }
  • 我已经在 tableviewCell 的 cellForRowAt 函数中重新加载了我的集合视图。还不够吗
  • 不,你不应该在cellForRowAt里面重新加载它在getDataFromFirebaseToTakeUsersis 里面做它
猜你喜欢
  • 2015-03-19
  • 2021-10-02
  • 2020-01-29
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多