2016-01-22 44 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

相關問題