简体   繁体   English

协议后的JSON解析

[英]JSON parsing after protocols

I implemented a carousel with a collection view. 我实现了具有集合视图的轮播。 I want to parse a JSON data into it, but the JSON parses after the necessary protocols. 我想将JSON数据解析到其中,但是JSON在必要的协议之后解析。 Because of this the array is empty when i try to configure the cells. 因此,当我尝试配置单元时,数组为空。

I know the problem occurs when I try to reload the data. 我知道在尝试重新加载数据时会出现问题。 When I do this in a table view controller I can just call tableView.reloadData() but I can't do this in the normal view controller I use. 当我在表视图控制器中执行此操作时,我只能调用tableView.reloadData(),但无法在我使用的普通视图控制器中执行此操作。

        self.view.setNeedsLayout()
        self.view.layoutIfNeeded()

This is the order when i print it: loadjson numberofsections cellforitemat cellforitemat parsejson 这是我打印时的顺序:loadjson numberofsections cellforitemat cellforitemat parsejson

When I do this in a table view controller I can just call tableView.reloadData() but I can't do this in the normal view controller 当我在表视图控制器中执行此操作时,我只能调用tableView.reloadData(),但无法在普通视图控制器中执行此操作

in normal vc you should have a property like 在普通的vc中,您应该具有以下属性

@IBOutlet weak var collectionView:UICollectionView!

and reload it when needed unlike the collection controller which encapsulates that property by default 并在需要时重新加载它,这与默认情况下封装该属性的collection控制器不同


Regrading the order of getting the data is asynchronous so check this CollectionView not display data after parsing JSON for a similar case 转换获取数据的顺序是异步的,因此类似情况下解析JSON后检查此CollectionView不显示数据


These 2 lines 这2行

self.view.setNeedsLayout()
self.view.layoutIfNeeded()

are totally irrelevant , they used for refreshing the UI and constraints 完全无关紧要,它们用于刷新UI和约束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM