【问题标题】:How can I reload data on my UICollectionView?如何在我的 UICollectionView 上重新加载数据?
【发布时间】:2015-05-14 16:22:04
【问题描述】:

每次调用 UICollectionView 时,我都需要重新加载图像。我目前有两个按钮(还有更多按钮),它们通过 Parse.com 查询连接并填充 UICollectionView。当我单击我的按钮时,图像被下载,并且 UICollectionView 有一个 segue,但图像仅在我点击“返回”并重新进入 UICollectionView 时显示。我试过使用 self.collectionView.reloadData() 但我只得到一个错误。

var images = [UIImage]()
var imageFiles = [PFFile]()
class CollectionCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

@IBAction func xy1Button(sender: AnyObject) {



}

@IBAction func xy2Button(sender: AnyObject) {
    var downloadImages = PFQuery(className: "XY2")
    downloadImages.whereKey("Expansion", equalTo: "XY2")
    downloadImages.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            println("Successfully retrieved \(objects!.count) images.")
            if let objects = objects as? [PFObject] {
                for object in objects {
                    imageFiles.append(object["Image"] as! PFFile)
                }
            }
        } else {
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }

    self.collectionView.reloadData()
//ERROR IS ABOVE: "Thread 1: EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0)"

    self.performSegueWithIdentifier("jumpToCollectionView", sender: self)
}

override func viewDidLoad() {



}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return imageFiles.count

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell: CardsCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CardsCollectionViewCell
    imageFiles[indexPath.row].getDataInBackgroundWithBlock{
        (imageData: NSData?, error: NSError?) -> Void in

        if error == nil {

            let image = UIImage(data: imageData!)

            cell.Img.image = image
        }
    }
    return cell
}
}

有什么帮助吗?!

【问题讨论】:

  • collectionView 第一次加载时是一个有效的对象吗(你确定它不为空)吗?
  • 这就是问题所在。第一次加载时为空。
  • 对了,需要初始化,有什么问题?

标签: ios swift parse-platform uicollectionview


【解决方案1】:

尝试在异步调度中包含self.collectionView.reloadData(),如下所示:

dispatch_async(dispatch_get_main_queue(), {
        self.collectionView.reloadData()
    })

希望对你有所帮助!

【讨论】:

    【解决方案2】:

    尝试调用 self.collectionview.collectionviewlayout.invalidatelayout() 来强制更新 CollectionView 的布局,从而刷新你的 UI。否则,请提供更多信息,因为您的问题不是很清楚。

    【讨论】:

    • 这充其量只是一个评论。
    • @Aggressor 不需要减号 .. 这是一个答案,就像我上面的那个人一样,你没有给减号...所以冷静哥们!
    猜你喜欢
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多