2016-12-01 57 views
0

我想说,我习惯于如何使用UITableViews并且不太熟悉UICollectionView。UICollectionView重新加载数据不会更新单元格

我正在做的基本上是

  1. 在屏幕上从网络添加次(包括收集视图)
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 
  • 负载数据和添加到数据源阵列
  • 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()如上所示

    调用没有出现在屏幕上,如果它是空白的重装数据之后。我已经尝试从主队列重新加载并插入/执行批量更新等。我只是很困惑如何让这个工作。在从网络加载之前,我必须知道项目数量吗?我怎样才能得到这个工作?

    这里如果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 
    } 
    
    +0

    检查你collectionView框架。我认为它现在为零。或者确定你为collectionView设置了数据源。 – nynohu

    +0

    你也可以添加一些代码吗? – gurmandeep

    +0

    我设置了一个数据源并且该帧不为零。我会发布一些代码 –

    回答

    0

    代码结束了与小区方法if条件的问题。它不喜欢它出于某种原因,所以我用了两个独立的视图控制器与容器视图,它的工作就像一个魅力与reloadData()

    相关问题