gpt4 book ai didi

ios - swift PFQueryTableViewController - 在呈现单元格之前删除重复的对象

转载 作者:行者123 更新时间:2023-11-29 01:17:51 26 4
gpt4 key购买 nike

我在 Parse 中有两个类,一个包含用户喜欢的图像的详细信息(“喜欢”),第二个类列出正在关注其他用户的用户(“关注”)。

在我的 PFQueryViewController 中,我能够在 queryForTable() 中创建一个执行以下操作的查询:

    //get username of users being followed
let query:PFQuery = PFQuery(className:"Follows")
query.whereKey("fromUser", equalTo: (PFUser.currentUser()?.username)!)

//get images of all followed users
let imageQuery:PFQuery = PFQuery(className: "Liked")
imageQuery.whereKey("username", matchesKey: "toUser", inQuery: query)

//get images current user liked
let userImagesQuery:PFQuery = PFQuery(className: "Liked")
userImagesQuery.whereKey("username", equalTo: (PFUser.currentUser()?.username)!)

//combine liked images of following and current user
let combinedQuery:PFQuery = PFQuery.orQueryWithSubqueries([imageQuery,userImagesQuery])


if(objects?.count == 0)
{
combinedQuery.cachePolicy = PFCachePolicy.CacheThenNetwork
}

combinedQuery.orderByDescending("createdAt")


return combinedQuery

通过查询每个单元格对象的imageID,我可以在cellForRowAtIndexPath中显示用户喜欢的图像以及正在关注的用户。 。但是,多个用户可以喜欢同一张图片,因此,如果一个用户和他们关注的某人喜欢同一张图片,则该图片将在表格中出现两次。

当查询基于 ImageID 键发现重复项时,是否有办法强制查询忽略对象?

我知道这可以通过执行查询并迭代对象来完成,但这在这种情况下不起作用,因为我需要在 queryForTable() 返回查询之前删除重复项.

最佳答案

我设法通过使用 objectsDidLoad() 解决了这个问题。这会在对象发送到表之前捕获它们,因此我能够遍历每个对象并删除任何重复项。

我将所有加载的对象存储到一个新数组中,如果发现重复项,则删除一个对象。然后,我将 cellForRowAtIndexPath 中的对象设为数组中的相应对象。

 override func objectsDidLoad(error: NSError?) {
super.objectsDidLoad(error)

displayedImages.removeAll()
imageObjects = NSMutableArray(array: objects!)

for item in imageObjects {
if displayedImages.count == 0 {
let image = item["imageid"] as! NSNumber
displayedImages.insert(image, atIndex: 0)
}else{
var count = 0
let image = item["imageid"] as! NSNumber
for imageToCheck in displayedImages {
if imageToCheck != image {
count++
}
}
if count != displayedImages.count {
imageObjects.removeObject(item)
}else{
displayedImages.insert(image, atIndex: 0)
}
}
}

self.tableView.reloadData()

}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return imageObjects.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! FeedTableViewCell

if let pfObject:PFObject = self.imageObjects.objectAtIndex(indexPath.row) as? PFObject {

//display images as before

关于ios - swift PFQueryTableViewController - 在呈现单元格之前删除重复的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34953453/

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