我正在尝试从服务器获取数据,但它没有进入集合视图:请看一下


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

getDataFromJSON()

let numberOfItemsPerRow:CGFloat = 2
let spacingBetweenCells:CGFloat = 25

let totalSpacing = (2 * 10) + ((numberOfItemsPerRow - 1) * spacingBetweenCells) //Amount of total spacing in a row

if let collection = self.collectionView{
let width = (collection.bounds.width - totalSpacing)/numberOfItemsPerRow
return CGSize(width: width, height: width)
}else{
return CGSize(width: 0, height: 0)
}
}

我正在colletion视图方法中调用服务器,但仍然无法工作?

您应该从sizeForItemAt方法中删除getDataFromJSON((调用,并将其放置到ViewController的viewWillAppear((方法中。

如果您要从服务器下载数据,它将是异步的。这意味着你的应用程序必须等待下载完成,然后重新加载收藏视图的显示。或者使用下载数据的任何其他视图。

所以在概念上:

  1. 根据客户的需要,开始在viewWillAppear((或viewDidLoad((中下载数据
  2. 下载完成后,重新加载collectionView和使用刚下载的数据的任何其他视图

最新更新