gpt4 book ai didi

ios - 从 Parse 检索对象时无法正确重新加载数据

转载 作者:行者123 更新时间:2023-11-29 01:47:28 24 4
gpt4 key购买 nike

我正在通过这种方式从“_User”类中检索数据:

我的声明..

 var userIds = [String]()
var userNames = [String]()
var profilePics = [PFFile]()
var gender = [String]()


var userQuery = PFUser.query()
userQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

if let objects = objects {

self.userIds.removeAll(keepCapacity: true)
self.userNames.removeAll(keepCapacity: true)
self.profilePics.removeAll(keepCapacity: true)

for object in objects {

if let user = object as? PFUser {
if user.objectId != PFUser.currentUser()?.objectId {

self.userIds.append(object["objectId"] as! userListTableViewCell) // getting an error here.. "unexpectedly found nil while unwrapping an Optional value"
self.userNames.append(object["fullName"] as! String!)
self.profilePics.append(object["profilePicture"] as! PFFile!)
self.gender.append(object["gender"] as! String!)

}


}
self.tableView.reloadData()
}


}




})

screen shots of my app[![][1] ] 1

在这里,当我点击用户“Rfdfbd”的关注按钮时,“取消关注”标题会自动出现在用户“Ihbj.....”上:/我该如何解决这个问题??

Screen Shots of my app..

我的 IBAction followButton 代码在这里:

@IBAction func followButtonTapped(sender: UIButton) {

println(sender.tag)

sender.setTitle("unfollow", forState: UIControlState.Normal)

let getOjbectByIdQuery = PFUser.query()
getOjbectByIdQuery!.whereKey("objectId", equalTo: userIds[sender.tag])
getOjbectByIdQuery!.getFirstObjectInBackgroundWithBlock { (foundObject: PFObject?, error: NSError?) -> Void in

if let object = foundObject {

var followers:PFObject = PFObject(className: "Followers")
followers["user"] = object
followers["follower"] = PFUser.currentUser()
followers.saveEventually()

}
}
}

我在这里使用 sender.tag 作为关注按钮..

最佳答案

我以前遇到过这个问题,并通过在每个单元格中嵌入一个按钮来解决它。在您的 UITableView 中,您应该尝试为每个单元格嵌入一个 UIButton

首先在单独的文件中创建一个自定义的 UITableViewCell。然后在您的自定义单元格中为您的 UIButton 拖动并制作一个 IBOutlet

class MyCustomCell: UITableViewCell{
@IBOutlet weak var followButton: UIButton!
var isFollowing:Bool = false
//Declare other cell attributes here like picture, name, gender
// ......
}

当您查询和收集单元格的数据时,您可以将它们存储在 UITableViewController 中的数组中。例如,var myCellArray = [MyCustomCell]()。然后你的 UITableViewController 看起来像这样:

var myCellArray = [userListTableViewCell]()

override func viewDidLoad(){
super.viewDidLoad()

var userQuery = PFUser.query()
userQuery.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in

if let usersArray = objects as! [PFUser] {

self.myCellArray.removeAll(keepCapacity: false)

for user in usersArray {

if let user = object as? PFUser {
if user.objectId != PFUser.currentUser()?.objectId {
var myCell = userListTableViewCell()
myCell.userID = user.objectId
myCell.username = user["fullName"] as! String
myCell.gender = user["gender"] as! String

var userPicture = user["profilePicure"] as? PFFile
var image = UIImage(data:userPicture!.getData()!)
myCell.displayPicture.image = image

myCellArray.append(myCell)
self.tableView.reloadData()

}
}
}
}
})
}


override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
myCellArray.count
}


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

var cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier") as! userListTableViewCell

//Edit the storyboard labels for each cell:
cell.username.text = myCellArray[indexPath.row].username
// etc....

//Embed a button with each cell
cell.followButton.layer.setValue(indexPath.row, forKey: "index")
cell.followButton.addTarget(self, action: "followButtonTapped:", for ControlEvents: UIControlEvents.TouchUpInside)

if (myCellArray[indexPath.row].isFollowing == false){
cell.followButton.setTitle("Follow", forState: .Normal)
}else{
cell.followButton.setTitle("Unfollow", forState: .Normal)
}
return cell
}

func followButtonTapped(sender: UIButton){
let cellIndex : Int = (sender.layer.valueForKey("index")) as! Int
//You now have the index of the cell whose play button was pressed so you can do something like
if (myCellArray[cellIndex].isFollowing == false){
myCellArray[cellIndex] = true
}else{
myCellArray[cellIndex] = false
}
self.tableView.reloadData()
}

关于ios - 从 Parse 检索对象时无法正确重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31735945/

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