gpt4 book ai didi

arrays - 在 Parse 中从指针数组中检索对象,然后在 Swift 中更新 uitableview 的更有效方法

转载 作者:搜寻专家 更新时间:2023-11-01 06:43:45 26 4
gpt4 key购买 nike

我有一个解析“事件”对象已经传递给了 viewController。无需查询。

这个“事件”对象包含指向解析“评论”对象的指针数组。

我对“评论”对象的两个属性感兴趣。两者都是名为“commentAuthor”和“commentText”的字符串。

我有一个带有原型(prototype)单元格的 UITableView,它需要显示“commentAuthor”和“commentText”字符串。

它目前正在运行,但我对自己的方式不满意,这是这样的:

var commentsArrayOfPointers: [AnyObject]?
var commentsArrayOfObjects = [PFObject]()

func dealWithCommentPointers() {

//get array of pointers from "Event" object
commentsArrayOfPointers = theEvent!["commentsArray"] as? [AnyObject]

commentsArrayOfObjects.removeAll()

//loop through each pointer, and "Fetch" its associated "Comment" object,
//then append that "Comment" object to my array of "Comment" objects
for comment in commentsArrayOfPointers! {

comment.fetchIfNeeded()

commentsArrayOfObjects.append(comment as! PFObject)

}

//print(commentsArrayOfObjects)

self.tableView.reloadData()

}

然后,我像这样构建我的表格单元格,从我之前填充的“commentsArrayOfObjects”中提取数据:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("commentCell",
forIndexPath: indexPath) as! commentCellClass

cell.commentCellNameLabel.text = commentsArrayOfObjects[indexPath.row]["commentAuthor"] as! String
cell.commentCellCommentLabel.text = commentsArrayOfObjects[indexPath.row]["commentText"] as! String

return cell
}

它有效,但我对在每个评论对象上使用 Parse 命令“fetchIfNeeded()”并不感到兴奋,因为它是一个同步函数,这意味着它会导致我的应用程序在显示表格之前暂停。我更喜欢使用异步函数,它允许加载 View Controller 的其余部分,然后评论会在可用时填充表。

我尝试执行 fetchInBackgroundIfNeeded() 这是一个异步命令,但当然它在构建原型(prototype)单元之前没有完成,导致崩溃(因为它正在寻找的数组数据为零)。

我还尝试在我需要的“事件”上设置一个 PFQuery(事件虽然我已经有了事件,但不需要重新查询它)并在“事件”的“commentsArray”属性上添加一个“includeKey” “类(class)。然后在查询结束时,我做了一个 self.tableView.reloadData(),但这也不起作用。这里似乎有一个更大的问题,因为查询似乎从未执行过,并且表重新加载,即使我已经注释掉了这样做的代码。这是一次尝试(我认为这更接近我的问题的正确解决方案):

func dealWithCommentPointers() {

print("inside the function")

var query = PFQuery(className: "Event")
let objectIdToLookup = self.theEvent?.objectId
query.whereKey("objectId", equalTo: objectIdToLookup!)
query.includeKey("commentsArray")

query.findObjectsInBackgroundWithBlock { (results, error) -> Void in

if error != nil {
print("Error found")
print(error)
}


else {

let eventArray = results as! [PFObject]

let theEventToDisplayCommentsFor = eventArray[0] //this should always be [0]

print("yo dude")

self.commentsArrayOfObjects = theEventToDisplayCommentsFor["commentArray"] as! [PFObject]

print("hey here i am!")

print(self.commentsArrayOfObjects.count)

self.tableView.reloadData() //even tried commenting this out, it is still called?!

}

}

}

那么,有什么想法吗?

最佳答案

为什么不预先将所有 Comment 对象包含在数组中?我的猜测是,您已经对之前的 View Controller 执行了查询,这就是您要传入 Event 对象的方式。执行该查询时,请使用 includeKey在数组上,因此所有 Comment 对象都随查询同时返回。

如果无法做到这一点,我建议向您的 TableView 添加几个函数,以便从 Parse 中异步抓取数据,然后重新加载表。 Here's an example for doing that which I've posted before .在该示例中有一个 UISearchController,但想法是相同的。

关于arrays - 在 Parse 中从指针数组中检索对象,然后在 Swift 中更新 uitableview 的更有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32766318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com