2016-01-22 64 views
0

我有2 UICollectionViewControllers与(据我所知)相同的代码和故事板设置。此应用程序使用Pinterest SDK,因此我一次下载电路板和引脚25。UICollectionView比dataSource.count显示更少的单元格

第一个CollectionViewController数据源(使用80个板子),将显示80个板子。第二个CollectionViewController(例如使用80个引脚)将只显示25个引脚。

我说的每一个print(indexPath.row)语句的cellForRowAtIndexPath

的正常运行CVC打印1,2,3,4,5,6,7,8,9,10等

第二届CVC打印1,2,3,4,5然后1,2,3,4,5 1,2,3,4,5等

任何人都可以弄清楚发生了什么事?编辑:下面的第2个CVC的代码,我已经去掉了所有不相关的代码:

class PinCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    //TODO: V2.0 FIND (1) duplicate pins (2) pins with out descriptions 

    var pins = [PDKPin]() 
    var imageArray = [UIImage]() 
    var boardID = String() 
    var boardName = String() 
    var fetchingMore = false 
    var currentResponseObject = PDKResponseObject() 
    var activityIndicatorView: ActivityIndicatorView! 



    override func viewDidLoad() { 
     super.viewDidLoad() 

     if let layout = collectionView?.collectionViewLayout as? PinterestLayout { 
      layout.delegate = self 
     } 

     getFirstPinsAndImages() 


    } 


    // MARK: UICollectionViewDataSource 

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 

     return 1 
    } 


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     return self.pins.count 
    } 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PinCollectionViewCell 

     cell.pinText?.text = self.pins[indexPath.row].descriptionText 
     cell.pinImage?.pin_updateWithProgress = true 


     if let pinImageURL = self.pins[indexPath.row].largestImage().url { 

      cell.pinImage?.pin_setImageFromURL(pinImageURL) 


     } 


       return cell 
    } 

    override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { 


      getRestOfPinsAndImages() 

    } 
    func loadImages(imageURLs: [NSURL], completion: [(url:NSURL, image:UIImage?)] -> Void) { 

     var downloads = imageURLs.count 
     let session = NSURLSession.sharedSession() 
     var images = [(url:NSURL, image:UIImage?)]() 

     for url in imageURLs { 

      let task = session.dataTaskWithURL(url) { (data, reponse, error) in 

       var image:UIImage? 
       if let data = data { 
        image = UIImage(data: data) 
       } 

       dispatch_sync(blocksDispatchQueue) { 
        //print("dispatch_Sync inside loadImages") 
        images.append((url:url, image:image)) 
        downloads-- 
       } 

       if downloads == 0 { 

        self.allImagesLoaded = true 
        // Alternative way of completing to ensure run on main thread 
        dispatch_async(dispatch_get_main_queue()) { completion(images) } 
       } 
      } 
      task.resume() 
     } 


    } 

    func getFirstPinsAndImages() { 


     PDKClient.sharedInstance().getBoardPins(boardID, fields: ["id", "image", "note", "link"], withSuccess: { (responseObject :PDKResponseObject!) -> Void in 

      self.currentResponseObject = responseObject 

      guard let pinsAsPDKPin = responseObject.pins() as? [PDKPin] else { 
           return 
      } 

      self.pins = pinsAsPDKPin 


      if let imageURLs = (pinsAsPDKPin.map { $0.largestImage().url }) as? [NSURL] { 


       //get images 
       self.loadImages(imageURLs, completion: {results in 
        // scan through results for the images found and 
        // add to array of images to display 
        for r in results { 
         if let image = r.image { 
          self.imageArray.append(image) 
         } 
        } 
       }) 
      } 


      dispatch_async(dispatch_get_main_queue(),{ 

       self.collectionView?.reloadData() 

      }) 



      }) { (err :NSError!) -> Void in 
       print("error NSError: \(err)") 
     } 



    } 





    func getRestOfPinsAndImages() { 




      if self.fetchingMore == false && self.currentResponseObject.hasNext() { 


       self.fetchingMore = true 

       self.currentResponseObject.loadNextWithSuccess({ (nextResponseObject :PDKResponseObject!) -> Void in 


        self.fetchingMore = false 
        self.currentResponseObject = nextResponseObject 

        guard let pinsAsPDKPin = nextResponseObject.pins() as? [PDKPin] else { return } 



        for pin in pinsAsPDKPin { 
         self.pins.append(pin) 
        } 

        if let imageURLs = (pinsAsPDKPin.map { $0.largestImage().url }) as? [NSURL] { 

         self.loadImages(imageURLs, completion: {results in 
          // scan through results for the images found and 
          // add to array of images to display 
          for r in results { 
           if let image = r.image { 
            self.imageArray.append(image) 
           } 
          } 



         }) 
        } 

        if !self.currentResponseObject.hasNext() { 

        } else { 
         self.getRestOfPinsAndImages() 
        } 

        dispatch_async(dispatch_get_main_queue(),{ 

         self.collectionView?.reloadData() 

        }) 

        }) { (err :NSError!) -> Void in 
         print("error NSError: \(err)") 
       } 

      } 
     } 




} 

extension PinCollectionViewController : PinterestLayoutDelegate { 
    // 1 
    func collectionView(collectionView:UICollectionView, heightForPhotoAtIndexPath indexPath: NSIndexPath, 
     withWidth width: CGFloat) -> CGFloat { 
      var photo = UIImage() 
      if allImagesLoaded { 
       photo = imageArray[indexPath.item] 
      } else { photo = UIImage(named: "testPic")!} 


      let boundingRect = CGRect(x: 0, y: 0, width: width, height: CGFloat(MAXFLOAT)) 
      let rect = AVMakeRectWithAspectRatioInsideRect(photo.size, boundingRect) 
      return rect.size.height 
    } 

    // 2 
    func collectionView(collectionView: UICollectionView, 
     heightForAnnotationAtIndexPath indexPath: NSIndexPath, withWidth width: CGFloat) -> CGFloat { 
      var photo = UIImage() 
      if allImagesLoaded { 
       photo = imageArray[indexPath.item] 
      } else { photo = UIImage(named: "testPic")!} 

      let annotationPadding = CGFloat(4) 
      let annotationHeaderHeight = CGFloat(17) 
      let font = UIFont(name: "AvenirNext-Regular", size: 10)! 
      let commentHeight = CGFloat(10.0) 
      let height = annotationPadding + annotationHeaderHeight + commentHeight + annotationPadding 
      return height 
    } 
} 
+1

没有你发布代码,任何人都难以评论。 – Michael

+0

这听起来像是你的numberOfSections方法返回的值大于1.所以它显示了每个部分中的5个项目,但是如果没有看到你用来填充视图的方法是无法帮助的。 –

+0

谢谢你们,这是一个很大的班级,所以我试图去掉所有与dataSource – GarySabo

回答

0

只要给我们更多的细节。 我想你给我们的信息不够。

尝试创建新项目,并编写简单的UIImageView, 并将其发布到iPhone 5s,6s,6s。

如果确定,那么你的项目有一些问题。 在这种情况下,您必须提供更多信息。

+0

无关的所有内容,请参阅我编辑的代码问题 – GarySabo

相关问题