本文介绍了从Parse检索对象时无法正确重新加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在以这种方式从_User类中检索数据:I am retrieving data from "_User" class this way:我的声明.. 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当我点击用户Rfdfbd的关注按钮时,自动unfollow标题出现在用户Ihbj .....上:/ how我能解决这个问题吗?here when i click on follow button for user "Rfdfbd" then automatically the "unfollow" title appears on user "Ihbj....." also :/ how can i fix this??我的IBAction followButton代码在这里:my IBAction followButton code is here:@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作为跟随按钮..I am using sender.tag for the follow button here..推荐答案之前我遇到过这个问题并通过在每个单元格中嵌入一个按钮来修复它。在你的 UITableView 里面,你应该尝试用 UIButton 嵌入每个单元格。I've had this problem before and fixed it by embedding a button in each cell. Inside your UITableView you should try embedding each cell with a UIButton.首先在单独的文件中创建自定义 UITableViewCell 。然后拖动并在自定义单元格中为 UIButton 制作一个 IBOutlet 。First make a custom UITableViewCell in a separate file. Then drag and make an IBOutlet for your UIButton inside your custom cell.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 将如下所示:When you query and gather the data for your cells, you can store them in an array in your UITableViewController. For example, var myCellArray = [MyCustomCell](). Then your UITableViewController will look something like this: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()} 这篇关于从Parse检索对象时无法正确重新加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-29 02:01
查看更多