UICollectionView重新加载数据不会更新单元格

时间:2016-12-01 04:15:26

标签: ios uicollectionview

我想通过说我已经习惯了UITableViews如何工作并且对UICollectionView不太熟悉来作为序言。

我基本上做的是

  1. 在屏幕上添加视图(包括集合视图)
  2. featuredCollectionView = UICollectionView(frame: CGRect(x: 0, y: featuredLabel.frame.origin.y + featuredLabel.frame.size.height, width: view.frame.size.width, height: 216), collectionViewLayout: layout)
            featuredCollectionView.delegate = self
            featuredCollectionView.dataSource = self
            featuredCollectionView.register(ItemCollectionViewCell.self, forCellWithReuseIdentifier:
            "cell")
            featuredCollectionView.backgroundColor = UIColor.fysGray
            featuredCollectionView.showsHorizontalScrollIndicator = false
            // add view to screen 
    
    1. 从网络加载数据并添加到数据源阵列
    2. func fetchPosts()
      {
        // note some libs have been changed for x purposes 
        NetworkClass.loadPosts()
        { (data, error) in 
          // parse into valid object 
          // add to datasource array
          self.featuredItems.append(item)
      
          // reload data 
          DispatchQueue.main.async
          {
              self.featuredCollectionView.reloadData()
          }
      
        }
      
        主要队列上的
      1. collectionView.reloadData()如上所示
      2. 调用重新加载数据后,屏幕上不显示任何内容,就像它是空白一样。我尝试从主队列重新加载并插入/执行批量更新等。我真的很困惑如何使其工作。从网络加载之前,我是否必须知道项目数量?我怎样才能使这个工作?

        这里是cellForItemAt的代码

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
        {
            var item: Item
            if collectionView == featuredCollectionView
            {
                item = featuredItems[indexPath.row]
            }
            else
            {
                item = recentlyViewedItems[indexPath.row]
            }
        
        
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ItemCollectionViewCell
            cell.item = item
            return cell
        }
        

1 个答案:

答案 0 :(得分:0)

结束了单元格方法中if条件的问题。由于某种原因,它不喜欢它,所以我使用了两个带有容器视图的独立视图控制器,它就像一个带有reloadData()的魅力

相关问题