【问题标题】:Loading TableViewDataSource after a method finish在方法完成后加载 TableViewDataSource
【发布时间】:2026-01-05 01:45:01
【问题描述】:

我想在从 parse 提取 Ui 后调用 TableViewData Sources 方法来查看 Ui。有了这个我可以获取

func loadImages() {

    var query = PFQuery(className: "TestClass")
    query.orderByDescending("objectId")


    query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
        if(error == nil){

            self.getImageData(objects as [PFObject])

        }
        else{
            println("Error in retrieving \(error)")
        }

    })//findObjectsInBackgroundWithblock - end


}

func getImageData(objects: [PFObject]) {

    for object in objects {

        let thumbNail = object["image"] as PFFile

        println(thumbNail)

        thumbNail.getDataInBackgroundWithBlock({
            (imageData: NSData!, error: NSError!) -> Void in
            if (error == nil) {
              var imageDic = NSMutableArray()
                self.image1 = UIImage(data:imageData)
                //image object implementation
                self.imageResources.append(self.image1!)


                println(self.image1)

                println(self.imageResources.count)


            }
            }, progressBlock: {(percentDone: CInt )-> Void in

        })//getDataInBackgroundWithBlock - end



    }//for - end


   self.tableView.reloadData()

但无法像这样将这些获取的数据填充到 tableview 中

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    println("in table view")
     println(self.imageResources.count)
    return imageResources.count+1;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("customCell") as CustomTableViewCell

    var (title, image) = items[indexPath.row]

   cell.loadItem(title: title, image: image)


    println("message : going upto this line")
    println(self.imageResources.count)


   var (image1) = imageResources[indexPath.row]

    cell.loadItem1(image1: image1)

return cell
}

然后在 loaditem 上,我试图显示图像,并且我已经编写了自己的数组来填充图像数组,但是在填充时我得到一个零值,因此无法设置它

非常感谢任何帮助!

【问题讨论】:

  • 您是否将tableview的委托/数据源与控制器连接起来。即,self.tableview.delegate = self; self.tableview.datasource = 自我; ?
  • 对不起suresh,但实际上我不能让你像我一样是新手,所以无法得到它,你能提供更多关于这方面的信息吗?
  • 尝试在 Parse 完成检索数据时调用 your_table_view.reloadData()
  • @ios 我尝试调用我自己的 tableview 数据,但仍然是那个空的索引缓冲区

标签: ios swift parse-platform ios8


【解决方案1】:

您有几个问题,都与并发有关 - 您的负载是在后台并行发生的。

第一个问题是在加载过程中使用self.image1作为临时变量——这个变量可能被多个线程并发访问。为此,您应该使用局部变量。

其次,您从多个线程追加到self.imageResources,但 Swift 数组不是线程安全的。

第三,您需要在加载完所有数据后在 tableview 上调用 reload,这现在不会发生,因为您在后台操作仍在进行时调用它。

最后,您的getImageData 函数正在后台队列上执行,您必须在主队列上执行 UI 操作(例如重新加载表)。

最简单的选择是将获取缩略图加载更改为同步调用 - 这意味着您的缩略图将按顺序加载,并且可能需要比多个并行任务更长的时间,但更易于管理 -

func getImageData(objects: [PFObject]) {

    for object in objects {

        let thumbNail = object["image"] as PFFile

        println(thumbNail)

        let imageData? = thumbNail.getData
        if (imageData != nil) {
                let image1 = UIImage(data:imageData!)
                //image object implementation
                self.imageResources.append(image1!)

                println(self.imageResources.count) 
       }

    }//for - end

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

更复杂的方法是使用调度组并保持并行图像加载。为此,您需要保护对共享数组的访问

【讨论】:

  • 你建议的代码我试过了,但在我的情况下似乎不起作用
  • 您的自定义单元类中loadItemloadItem1 的代码是什么?您可以将其添加到您的问题中吗?你现在在日志中得到了什么?
  • 为什么在 numberOfRowsInSection 中返回 count+1?您是否设置了断点以确认您的表函数正在被调用?
  • func loadItem(#title: String, image: String) { backgroundImage.image = UIImage(named: image) titleLabel.text = title } func loadItem1(#image1 :UIImage) { backgroundImage.image= image1 } 在 loadItem 我正在加载我自己的本地存储图像返回 count+1 只是为了调试目的而添加的
  • 我不断收到的错误是从我发布到 dat 的那一天,即致命错误:无法索引空缓冲区