只有1节显示PFQueryCollectionViewController

时间:2015-05-16 21:21:43

标签: ios swift parse-platform uicollectionview

我将图片从Parse中拉出并将其加载到PFQueryCollectionViewController。查询运行并拉出图像。然而,它只返回一行图像而不是多行图像。有很多图像。我已经被困在这一段时间了一段时间,这让我发疯了。

如果我设置了基本override func numberOfSectionsInCollectionViewoverride func numberOfItemsInCollectionView方法,则在加载显示[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array的视图时会返回错误

同样,会出现一行图像,但不会继续显示第二行。

class MasterCollection: PFQueryCollectionViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

@IBOutlet var cView: UICollectionView?

override func viewDidLoad() {
    super.viewDidLoad()

    self.objectsPerPage = 77

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)
    layout.itemSize = CGSize(width: 100, height: 100)
    cView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    cView!.dataSource = self
    cView!.delegate = self
    self.cView!.registerClass(MasterCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    cView?.backgroundColor = UIColor.greenColor()
    self.view.addSubview(cView!)!
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

 /*override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 100

}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}*/

override func queryForCollection() -> PFQuery {
    println("query ran")

    var query: PFQuery = PFQuery(className: questionClass)
    if (objects.count == 0) {
        query.cachePolicy = PFCachePolicy.CacheThenNetwork
    }
    query.orderByAscending(objectId)

    println(query)
    return query
}

  override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFCollectionViewCell? {

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

    cell.parseObject = object

    if let pfObject = object {

        let image = pfObject[questionImage] as? PFFile
        cell.collectionCellImageView?.file = image
        cell.collectionCellImageView?.loadInBackground()
    }
    return cell
}

enter image description here

这与我的问题相似,但我没有使用TableView:Swift index 0 beyond bounds for empty array in tableview

1 个答案:

答案 0 :(得分:0)

问题在于尝试覆盖在Storyboard上实现的布局设置。在Attributes Inspector中设置PFQueryCollectionViewController最常量属性,而不是以编程方式设置布局和单元属性的规范。

当然,这遵循以下规则:10次中有9次比您尝试制作它更简单。