2015-05-16 58 views
0

我將圖像從Parse拉出並加載到PFQueryCollectionViewController。查詢運行並且圖像被拉出。但是它只返回一行圖像而不是多行圖像。有很多圖像。我一直在困擾這一段時間,這讓我發瘋。只有1個部分顯示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

回答

0

的問題是在試圖覆蓋對故事板實現的佈局設置。代替在PFQueryCollectionViewController中以編程方式設置佈局和單元格屬性的規範,大多數常量屬性都在屬性檢查器中設置。

當然,這遵循的規則是10倍的9倍,比你想要做的更簡單。