简体   繁体   English

使用数组数据更新 TableView

[英]Update TableView with Array data

I have a PFQuery in viewDidLoad called in getUserPicPostData .我在getUserPicPostData调用viewDidLoad中有一个PFQuery The output shows there are 3 images in the array count. output 显示阵列计数中有 3 个图像。

ViewdidLoad: ViewdidLoad:

func getUserPicPostData() {
    let query = PFQuery(className: "PicPost")
    query.whereKey("Username", equalTo: usernameSelected)
    query.findObjectsInBackground(block: { (objects: [PFObject]?,error: Error?) in
        if let objects = objects {
            for object in objects {
                self.picLikeArray.append(object["UserLikes"] as! Int)
                self.picTimeArray.append(object["UserTime"] as! String)
                self.picObjectIdArray.append(object.objectId!)

                let userImageFile = object["ImageFile"] as? PFFileObject

                userImageFile?.getDataInBackground(block: { (imageData: Data?, error: Error?) in
                    if error == nil {
                        self.picPostArray.append(UIImage(data: imageData!)!) 
                        print(self.picPostArray.count)
                    } 
                    else {
                        print(error)
                    }
                })
            } 
        }
    })
}

Then I want to tell my cells to take the inputs of the imageArray.然后我想告诉我的细胞接受 imageArray 的输入。 PicPostArray is now nil?? PicPostArray 现在为零??

cellForRowAtIndexPath: cellForRowAtIndexPath:

print(picPostArray)
cell.imageView1.imageView!.image = picPostArray[0]
cell.imageView2.imageView!.image = picPostArray[1]
cell.imageView3.imageView!.image = picPostArray[2]

When you reload the table, your picPostArray is nil because it's not setted yet.当您重新加载表格时,您的picPostArray为 nil,因为它尚未设置。 This is due to your asynchronous query.findObjectsInBackground .这是由于您的异步query.findObjectsInBackground To solve this, you can reload the table only when picPostArray is setted:要解决这个问题,您只能在设置picPostArray时重新加载表:

var picPostArray: [UIImage] = [] {
    didSet {
        yourTableView.reloadData()
    }
}

Just reload the tableView after adding the image on the list.只需在列表中添加图像后重新加载 tableView。

picPostArray initialization will be picPostArray初始化将是

var picPostArray:[UIImage] = [] // picPostArray not nil 

And then reload the tableView when the image is set on the array.然后在数组上设置图像时重新加载 tableView。

self.picPostArray.append(UIImage(data: imageData!)!) 
self.tableView.reloadData()

Due to an asynchronous request, you need to reload the tableView.由于是异步请求,需要重新加载tableView。

Hope this will solve this issue.希望这能解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM