在表格视图中的所有单元格都已加载之后,重新加载表格视图单元格内的集合视图 [英] Reloading collection view inside a table view cell happens after all cells in table view have been loaded

查看:21
本文介绍了在表格视图中的所有单元格都已加载之后,重新加载表格视图单元格内的集合视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在表格视图的每个表格视图单元格内实现一个集合视图,但无法让集合视图在正确的时间重新加载.看起来集合视图会在所有表格视图单元格加载后自行重新加载,而不是每次都有新单元格出列到表格视图中,因为我试图这样做.

I am trying to implement a collection view inside each table view cell in a table view, but am having trouble getting the collection view to reload at the right time. It looks like the collection view reloads itself after all the table view cells have been loaded, not each time a new cell is dequeued to the table view as I'm trying to make it do.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCellWithIdentifier("LWTableViewCell") as! LWTableViewCell
    
    cell.collectionView.delegate = self
    cell.collectionView.dataSource = self
    
    if dataIsReady == 1 {
        setIndex = indexPath.row
        print("in table view cellForRowAtIndexPath, setting setIndex at (setIndex)")
        cell.collectionView.reloadData()
    }
    return cell
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    if dataIsReady == 1 {
        print("In collection view numberOfItemsInSection setIndex is (setIndex)")
        return self.model.sets[setIndex].subsets!.count
    }
    else { return 0 }   
}

在终端中,我得到以下信息:

In terminal, I get the following:

在表格视图 cellForRowAtIndexPath 中,设置 setIndex 为 0

in table view cellForRowAtIndexPath, setting setIndex at 0

在表格视图 cellForRowAtIndexPath 中,设置 setIndex 为 1

in table view cellForRowAtIndexPath, setting setIndex at 1

在表格视图 cellForRowAtIndexPath 中,设置 setIndex 为 2

in table view cellForRowAtIndexPath, setting setIndex at 2

在集合视图中 numberOfItemsInSection setIndex 为 2

In collection view numberOfItemsInSection setIndex is 2

在集合视图中 numberOfItemsInSection setIndex 为 2

In collection view numberOfItemsInSection setIndex is 2

在集合视图中 numberOfItemsInSection setIndex 为 2

In collection view numberOfItemsInSection setIndex is 2

而我希望看到以下事件顺序(假设集合视图重新加载方法是/应该在每次新的表视图单元出列时调用).

whereas I would expect to see the following order of events (given that the collection view reload method is/should be called each time a new table view cell is dequeued).

在表格视图 cellForRowAtIndexPath 中,设置 setIndex 为 0

in table view cellForRowAtIndexPath, setting setIndex at 0

在集合视图中 numberOfItemsInSection setIndex 为 0

In collection view numberOfItemsInSection setIndex is 0

在表格视图 cellForRowAtIndexPath 中,设置 setIndex 为 1

in table view cellForRowAtIndexPath, setting setIndex at 1

在集合视图中 numberOfItemsInSection setIndex 为 1

In collection view numberOfItemsInSection setIndex is 1

在表格视图 cellForRowAtIndexPath 中,设置 setIndex 为 2

in table view cellForRowAtIndexPath, setting setIndex at 2

在集合视图中 numberOfItemsInSection setIndex 为 2

In collection view numberOfItemsInSection setIndex is 2

任何关于为什么会发生这种行为以及如何解决它的建议将不胜感激!

Any suggestions on why this behavior is happening, and how to potentially fix it would be much appreciated!

我查看了有关该主题的其他一些问题,并且知道有一些关于该主题的教程(例如,https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/),但之前开始用不同的方法做事,我想了解为什么上面的方法似乎不起作用/可以做些什么来使它起作用.

I have looked through some other questions on the topic, and am aware there are some tutorials on the topic (e.g., https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/), but before starting to do things with a different approach, I'd like to understand why the above doesn't seem to work / what could be done to make it work.

推荐答案

你不能依赖调用的顺序.当前,您正在尝试将 setIndex 从表视图的 cellForRowAtIndexPath 传递给集合视图的委托方法.解决此问题的一种简单方法是,不要使用单个变量来传递行号,而是将其传递到集合视图的 tag 属性中.然后每个集合视图都会知道它的相关行号.即

You cannot rely on the sequence of the calls. Currently you are trying to pass setIndex from the table view's cellForRowAtIndexPath to the collection view's delegate methods. A simple way to fix this is rather than using a single variable to pass the row number, instead pass it in the collection view's tag property. Then each collection view will know it's relevant row number. i.e.

在表格视图的cellForRowAtIndexPath中:

cell.collectionView.tag = indexPath.row

然后在collection view的numberOfItemsInSection中:

And then in the collection view's numberOfItemsInSection:

return self.model.sets[collectionView.tag].subsets!.count

还要注意,您不需要在这些方法中测试 dataIsReady.此代码仅在表格视图的 tableView:numberOfRowsInSection: 中需要.当数据未准备好时,它应该返回 0(即没有行可显示).因此 table view 的 cellForRowAtIndexPath 永远不会被调用,并且由于没有行,所以没有集合视图,所以它们的 numberOfItemsInSection 永远不会被调用.

Note also that you should not need to test dataIsReady in these methods. This code is only needed in the table view's tableView:numberOfRowsInSection:. When the data is not ready, it should return 0 (ie. no rows to display). Therefore the table view's cellForRowAtIndexPath will never be called, and since there are no rows, there are no collection views, so their numberOfItemsInSection will never be called.

这篇关于在表格视图中的所有单元格都已加载之后,重新加载表格视图单元格内的集合视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆