无法从PFQueryCollectionViewController Swift iOS中的Parse加载数据

时间:2015-03-12 10:29:57

标签: ios parsing swift collectionview

我在将数据从Parse加载到PFQueryCollectionViewController时遇到问题。这是我的WaterfallFeedCollectionViewController类:

 import UIKit
 import Parse

class WaterfallFeedCollectionViewController: PFQueryCollectionViewController, CollectionViewWaterfallLayoutDelegate {

@IBOutlet var theCollectionView: UICollectionView!
//Random high Cells
lazy var cellSizes: [CGSize] = {
    var _cellSizes = [CGSize]()

    for _ in 0...20 {
        let random = Int(arc4random_uniform((UInt32(100))))

        _cellSizes.append(CGSize(width: 140, height: 150 + random))
    }

    return _cellSizes
    }()

init!(style: CollectionViewWaterfallLayout, className: String!) {
    super.init(collectionViewLayout: style, className: className)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    // Configure the PFQueryCollectionView
    self.parseClassName = "foodEntry"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = true
    self.objectsPerPage = 15
}


override func queryForCollection() -> PFQuery {
    var query = PFQuery(className: "foodEntry")
    query.orderByAscending("createdAt")

    return query
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let layout = CollectionViewWaterfallLayout()
    layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    layout.headerInset = UIEdgeInsetsMake(0, 0, 0, 0)
    layout.headerHeight = 0
    layout.footerHeight = 0
    layout.minimumColumnSpacing = 10
    layout.minimumInteritemSpacing = 10

    theCollectionView.collectionViewLayout = layout
    theCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: CollectionViewWaterfallElementKindSectionHeader, withReuseIdentifier: "Header")
    theCollectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: CollectionViewWaterfallElementKindSectionFooter, withReuseIdentifier: "Footer")
}

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

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

    var cell = theCollectionView.dequeueReusableCellWithReuseIdentifier("myWaterfallCell", forIndexPath: indexPath) as myWaterfallCollectionViewCell
    cell.FoodNameLabel.text = object["FoodName"] as? String

    var text = object["FoodName"] as? String
    println("\(text) funktioniert")

    return cell
}

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    var reusableView: UICollectionReusableView? = nil

    if kind == CollectionViewWaterfallElementKindSectionHeader {
        reusableView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as? UICollectionReusableView

        if let view = reusableView {
            view.backgroundColor = UIColor.redColor()
        }
    }
    else if kind == CollectionViewWaterfallElementKindSectionFooter {
        reusableView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as? UICollectionReusableView
        if let view = reusableView {
            view.backgroundColor = UIColor.blueColor()
        }
    }

    return reusableView!
}

// MARK: WaterfallLayoutDelegate

override func collectionView(theCollectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return cellSizes[indexPath.item]
}

}

它总是返回零。关于CollectionView,Parse的文档非常糟糕。我找到了一个示例项目,并试图用它和它们在PFQueryTableView中加载数据的方式来解决它。拉动刷新工作,所以我猜它正确的方式。

1 个答案:

答案 0 :(得分:0)

上面的代码实际上有效。我刚刚通过pod添加了ParseUI框架的问题。