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

我正在以这种方式从“_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() } } }) 

我的应用程序的屏幕截图[![] [1] ] 1

在这里,当我点击用户“Rfdfbd”的关注按钮,然后自动“取消关注”标题出现在用户“Ihbj …..”上:/我怎么能解决这个问题?

我的应用程序的屏幕截图..

我的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() } 
Interesting Posts