Ios 如何在解析查询结束时仅重新加载一次tableview?
基本上,我已经构建了一个带有Ios 如何在解析查询结束时仅重新加载一次tableview?,ios,swift,parse-platform,Ios,Swift,Parse Platform,基本上,我已经构建了一个带有PFQuery的tableviewcontroller来加载每个单元格的信息并将其存储在一个数组中。有两个需要解析的调用,一个用于将用户名存储到数组中,另一个用于检查当前用户是否在“跟踪”它们 如果我在另一个位置调用reloadData(),该表将无法正确显示。这使得我的表加载效率低下..因为每次检查“isfollowing”变量时表都会重新加载。我如何让它只重新加载一次?在添加了所有“isfollowing”变量之后?你能一步一步地概述一下解决方案吗?这样我就能大致
PFQuery
的tableviewcontroller
来加载每个单元格的信息并将其存储在一个数组中。有两个需要解析的调用,一个用于将用户名存储到数组中,另一个用于检查当前用户是否在“跟踪”它们
如果我在另一个位置调用reloadData()
,该表将无法正确显示。这使得我的表加载效率低下..因为每次检查“isfollowing”变量时表都会重新加载。我如何让它只重新加载一次?在添加了所有“isfollowing”变量之后?你能一步一步地概述一下解决方案吗?这样我就能大致了解方向和学习内容
编辑2:
在上面的代码中,我查询以检查currentUser正在跟踪哪些用户。如果它们在跟踪它们,我会将“true”附加到数组“following”。下面的代码是我选中tableviewcells以显示它们正在跟踪其他用户所做的操作。在我的原始代码中,所有单元格在上下滚动几次后都会被选中,为什么
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
if following.count > indexPath.row {
if following[indexPath.row] == true {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
}
cell.textLabel.text = users[indexPath.row]
return cell
}
编辑:最初发布的代码错误。使用
findObjects
同步调用并将其放入我们自己的队列可能会有所帮助
// queue is declared as an instance property
let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL)
var query = PFUser.query()
dispatch_async(queue) {
dispatch_async(dispatch_get_main_queue()) {
self.users.removeAll(keepCapacity: true) // users is an array declared globally
}
for user in query.findObjects() as [PFUser] {
var isFollowing = false
if user.username != PFUser.currentUser().username {
dispatch_async(dispatch_get_main_queue()) {
self.users.append(user.username)
}
var query = PFQuery(className:"followers")
query.whereKey("follower", equalTo:PFUser.currentUser().username)
query.whereKey("following", equalTo:user.username)
// call findObjects synchronously
if let objects = query.findObjects() {
if !objects.isEmpty {
isFollowing = true
}
dispatch_async(dispatch_get_main_queue()) {
self.following.append(isFollowing)
}
}
}
}
// notify table view to reload
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
您可以创建UITableViewCell子类并覆盖其prepareforeuse
以重置accessoryType
class MyTableView: UITableViewCell {
override func prepareForReuse() {
accessoryType = .None
}
}
在代码中,您没有处理[indexPath.row]之后的
案例正确
。如果不想将UITableViewCell子类化,也可以在那里重置复选标记。将重新加载数据
发送到主线程,并确保以线程安全的方式访问用户名
和用户图像文件
。我最初发布了错误的代码。请再快速看一下!感谢为解决此问题,我是否使用coreData保存整个查询的结果,然后使用NSNotificationCenter调用reloadData()?感谢Swipesight。刚在谷歌上搜索了“dispatch\u async(dispatch\u get\u main\u queue()”,有点困惑。它是否在查询块的后台异步运行append()。然后调用dispatch_async(dispatch_get_main_queue())仅在上一个dispatch_async(dispatch_get_main_queue())完成时再次运行reloadData()?在查询完成之前,它不会运行重载数据吗?感谢您对初学者的帮助事实上,您的代码出于某种原因使每一个“isFollowing”都成为真的(tableview现在是空的,而活动指示器在状态栏中无休止地运行。再次感谢您的帮助。@user3483697在查看PFQuery的源代码后,传递给FindObjectsInBackgroundithBlock
的块将在调用线程上执行,因此我以前的代码出现死锁,请再次检查我的代码.关于性能问题,有一些方法可以让它看起来更好。但我认为这是一个不同的问题,可能不是在这个帖子上回答的好地方。
class MyTableView: UITableViewCell {
override func prepareForReuse() {
accessoryType = .None
}
}