【问题标题】:Why is reloadData() not working in my app?为什么 reloadData() 在我的应用程序中不起作用?
【发布时间】:2015-05-11 16:37:40
【问题描述】:

我正在尝试从 Parse.com 下载一组图像到 UICollectionView。除了 reloadData() 函数外,一切正常。在应用程序中,我可以点击后退按钮并重新进入 UICollectionView 并且所有图像都在那里。我只是不知道我在使用 reloadData() 函数时做错了什么。

var images = [UIImage]()
var imageFiles = [PFFile]()

class CollectionCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@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) cards.")
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        imageFiles.append(object["Image"] as! PFFile)
                        dispatch_async(dispatch_get_main_queue() ^{
                            self.collectionView.reloadData()
                            });
                        //ERROR BREAKPOINT HERE:'Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)'
                    }
                }
            } else {
                println("Error: \(error!) \(error!.userInfo!)")
            }    
      }
}   
override func viewDidLoad() {
    super.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
    //cell.cardsImg.image = UIImage(named: cards[indexPath.row] as String)
    //return cell
    imageFiles[indexPath.row].getDataInBackgroundWithBlock{
        (imageData: NSData?, error: NSError?) -> Void in

        if error == nil {

            let image = UIImage(data: imageData!)

            cell.cardsImg.image = image
        }


    }
    return cell
}
}

【问题讨论】:

  • 你在哪里定义self.collectionView
  • 像 sahara 说的那样在 main_queue 中运行它 :) 这并不总是很直观,但通常当 UI 更改不更新或需要很长时间才能更新时,然后尝试在主队列中运行代码
  • 您似乎没有collectionView 的出口,因此self.collectionView 没有这样的属性。您定义的委托函数不需要一个,因为它是作为参数传入的,但要调用 reloadData(),您需要。
  • 是的,您可能使用了 Nib 或 Storyboard,请确保您的 IBOutlet collectionView 已链接到 Nib/Storyboard 文件。并在主线程上重新加载
  • CollectionCollectionViewController 可能应该是 UICollectionViewController 的子类,而不是 UIViewController。

标签: ios swift parse-platform uicollectionview


【解决方案1】:

reloadData 需要在主线程中执行以更新 UI。你可以试试这个

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

正如@Grimxn 建议的那样,您应该检查您的collectionView IBOutlet 是否已连接到情节提要或xib 文件中。

【讨论】:

  • 他说如果他回去重新进入屏幕,集合视图就会更新。所以我认为他已经设置了出口。无论如何谢谢你的评论。我会更新答案。
  • ...如果他没有出路会发生什么。委托还是会提供数据,即使他没有出路,他也不能自己调用​​reloadData(),他必须依靠系统为他重新加载集合......
  • 根据parse.com/questions/…,所有 Parse 完成块在后台线程上调用。这将如何解释 编译器错误 'Cannot invoke 'reloadData()' with no arguments'
  • 我还是有问题。我应该把这段代码放在哪里?
  • 我设法使用了DispatchQueue.main.async(execute: { code }),但我仍然在collectionView(_ :indexPath: ) 中看不到任何结果,而numberOfSections(collectionView:) -> Int 能够返回我正在创建的正确行数。我将我的数据放入数组字典中:` var usersPayments:[String:[String]] = ["Name":["Amount"]] `
【解决方案2】:

即使我打算这样做,但我只是忘记了,而且我没有在您的代码中看到它,因此这也可能是您的问题。我从未将 ViewController 设置为 collectionView.delegate 和 collectionView.dataSource

我使用了 Image Pickers、TableViews 和其他需要委托的 UI 组件,但这次只是忽略了它。

这是我的代码:

class MainViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self
    }

    func setData(data: String) {
        DispatchQueue.main.async {
            collectionView.reloadData()
        }
    }

    /* DELEGATE METHODS
    .........
    END DELEGATE METHODS */
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多