【发布时间】:2017-11-09 14:33:48
【问题描述】:
我在 UITableViewCell 中有一个 UICollectionView。您可以参考here的图片
如果发生任何更新,我想重新加载 collectionView。
我做了一些研究,发现了这个:
- how to reload a collectionview that is inside a tableviewcell
- Reloading collection view inside a table view cell happens after all cells in table view have been loaded
- UICollectionView not updating inside UITableViewCell
我在cellForRowAt 将@IBOutlet weak var collectionView: UICollectionView! 从UITableViewCell 呼叫到UITableViewController。
代码如下:
var refreshNow: Bool = false
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.allCardCell, for: indexPath) as! AllCardTableViewCell
if refreshNow {
cell.collectionView.reloadData()
refreshNow = false
}
cell.collectionView.collectionViewLayout.invalidateLayout()
return cell
}
如果用户点击 UIAlertAction 上的Ok:
let alert = UIAlertController(title: "Success", message: "Card successfully added", preferredStyle: .alert)
let action = UIAlertAction(title: "Ok", style: .default) { (action) in
self.refreshNow = true
self.tableView.reloadData()
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
我之所以放refreshNow是为了防止应用程序滞后和缓慢。但是如果发生任何变化,仍然没有更新。
问题是collectionView没有刷新。但是我调试的时候,是通过cell.collectionView.reloadData().
更新/更改仅在我重新启动应用程序时发生。我希望它是所谓的real-times 更新。
非常感谢任何帮助,非常感谢。
图片来源:How to use StoryBoard quick build a collectionView inside UITableViewCell
【问题讨论】:
-
尝试使用调度队列在主线程上调用重载数据。
-
@TusharSharma 哪一部分?在
cellForRowAt? -
是的,在那里调用一个调度队列,并在其中调用重新加载数据行。
-
@TusharSharma 还是一样。
if refreshNow { DispatchQueue.main.async { cell.collectionView.reloadData() } refreshNow = false } -
是的,尝试一次。
标签: ios swift uitableview swift3 uicollectionviewcell