UICollectionView显示的单元格数少于dataSource.count

时间:2016-01-22 01:01:38

标签: ios swift uicollectionview

我有2 UICollectionViewControllers(据我所知)相同的代码和故事板设置。这个应用程序使用Pinterest SDK,所以我一次下载25个板和引脚。

第一个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等

任何人都可以知道发生了什么事吗?编辑:下面的第二个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 个答案:

答案 0 :(得分:0)

请告诉我们更多细节。 我认为你没有给我们足够的信息。

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

如果没问题,那么您的项目有问题。 在这种情况下,您必须向我们提供更多信息。