使用异步数据填充UICollectionView?

时间:2015-05-19 16:11:39

标签: ios swift asynchronous uicollectionview

我正在尝试使用从RSS源检索到的数据填充UICollectionView,但是当我需要重新加载时,UICollectionViewnil。以下是我正在尝试的内容:

class ViewController: UITabBarController {

    let request = NSURLRequest(URL: NSURL(string: "http://example.com/feed")!)
    var itemRSS: [RSSItem]? = nil

    @IBOutlet var currentCollection: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        RSSParser.parseFeedForRequest(request, callback: {
            (feed, error) -> Void in

            if let f = feed {
                self.itemRSS = f.items
                self.currentCollection.reloadData()
            }
        })

    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemRSS != nil ? itemRSS!.count : 0
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! PostViewCell
        let item = itemRSS?[indexPath.row]

        cell.titleLabel.text = item!.title

        return cell
    }

当它到达self.currentCollection.reloadData()中的viewWillAppear时,它会崩溃,说它是nil。有更好的方法吗?

0 个答案:

没有答案