【问题标题】:Not able to reload data properly when retrieving objects from Parse从 Parse 检索对象时无法正确重新加载数据
【发布时间】:2015-07-30 23:52:37
【问题描述】:

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

当我点击用户“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 作为关注按钮..

【问题讨论】:

  • 这似乎您在不同的单元格中有相同的按钮引用。这就是为什么您更新按钮内容它会影响按钮引用的所有位置。
  • 你能详细说明你的答案吗??

标签: ios swift uitableview parse-platform


【解决方案1】:

我以前遇到过这个问题,并通过在每个单元格中嵌入一个按钮来解决它。在您的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()
}

【讨论】:

  • 你能详细说明“var myCellArray = [MyCustomCell]()”吗??
  • 你可以在你的函数之前在你的 UITableViewController 类中创建它。在 viewDidLoad 上,您应该执行 Parse query 并遍历 Parse 类中的每个 PFObject。在循环中,您可以创建一个 MyCustomCell 并为其提供 Parse query 中的属性。完成查询后,请致电 self.tableView.reloadData() 加载您的单元格。
  • 我已经在我的查询和其余的 PFObjects我解释过的其他数组..比如用户名、profilePics、性别..:/我做错了什么??
  • 在查询时尝试使用query.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in }) 函数。这样,您将获得要循环的对象的 array
  • 您编辑的查询与我刚刚给您的声明不匹配。您没有从查询中获取对象数组。
猜你喜欢
  • 2015-08-09
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多