ios - `numberOfItemsInSection` 仅在添加数组中的第一项后调用,将项目限制为只有一个

标签 ios swift uitableview uicollectionview

目前我有一个 UICollectionvView,它从相应的 UITableView 单元格获取信息,如下所示:

//TableView
var knownForArray: [UIImage] = []
var knownForExtendedArray: [CastExtendedData] = []

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = peopleImagesTableView.dequeueReusableCell(withIdentifier: knownForCellIdentifier) as! KnownForCell

    print("Known for array count at cell:\(self.knownForArray.count)")

    cell.imageDelegete = self
    cell.knownForExtendedArray = self.knownForExtendedArray
    cell.knownForArray = self.knownForArray
    self.peopleImagesTableView.rowHeight = 150

    return cell
}

//CollectionView 

var knownForArray: [UIImage] = []
var knownForExtendedArray: [CastExtendedData] = []

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print("Known for array count collectionview numberOfRowsInSection:\(self.knownForArray.count)")

    return knownForArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = knownForCollectionView.dequeueReusableCell(withReuseIdentifier: knownForReuseIdentifier, for: indexPath) as! KnownForCollectionViewCell

    if let knownPoster = knownForExtendedArray[indexPath.row].poster{

    DispatchQueue.main.async {

    print("collectionview knownForarray: \(self.knownForArray.count)")

    cell.knownForImages.sd_setImage(with: URL(string: "\(baseImageURL)\(knownPoster)"))
    }

  }
  return cell
}

通过添加 print 语句,只要第一个项目添加到 TableView 的 knownForArray 中,我就能知道这一点。它触发collectionview的numberOfItemsInSection,它将节中的总项目数限制为1。在完成检索所有JSON数据后,tableview的knownForArray可以有50多个项目。

最佳答案

每当网络调用完成时,您都需要调用collectionView.reloadData()

关于ios - `numberOfItemsInSection` 仅在添加数组中的第一项后调用,将项目限制为只有一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42936374/

相关文章:

android - 这将是 Swift 3 中 addValueEventListener 的等效命令

string - 在 Swift 中转换字符和代码点

ios - PFQueryTableViewController 两部分有两个数据源

ios - UITableView 仅在第二次尝试时将数据发送到 Detail View Controller

java - APP IOS 和 ANDROID 网站

html - 如何设置 iOS6 文件输入控件的样式?

ios - 字符串到字典的转换总是有 Nil 值

ios - 图像仅在 swift 3 中的 Whatsapp 上共享

swift - 使用相同的参数递归调用函数

iphone - 具有多列的 UITableView