从 Parse 检索对象时无法正确重新加载数据 [英] Not able to reload data properly when retrieving objects from Parse

查看:14
本文介绍了从 Parse 检索对象时无法正确重新加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这种方式从_User"类中检索数据:

我的声明..

 var userIds = [String]()var userNames = [String]()var profilePics = [PFFile]()var 性别 = [String]()var userQuery = PFUser.query()userQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in如果让对象 = 对象 {self.userIds.removeAll(keepCapacity: true)self.userNames.removeAll(keepCapacity: true)self.profilePics.removeAll(keepCapacity: true)对于对象中的对象{如果让用户 = 对象为?PF用户{如果 user.objectId != PFUser.currentUser()?.objectId {self.userIds.append(object["objectId"] as! userListTableViewCell)//在这里得到一个错误..在解开一个可选值时意外发现 nil"self.userNames.append(object["fullName"] as!String!)self.profilePics.append(object["profilePicture"] as!PFFile!)self.gender.append(object["gender"] as!String!)}}self.tableView.reloadData()}}})

]

我的 IBAction followButton 代码在这里:

@IBAction func followButtonTapped(sender: UIButton) {println(sender.tag)sender.setTitle("取消关注", forState: UIControlState.Normal)让 getOjbectByIdQuery = PFUser.query()getOjbectByIdQuery!.whereKey("objectId", equalTo: userIds[sender.tag])getOjbectByIdQuery!.getFirstObjectInBackgroundWithBlock { (foundObject: PFObject?, error: NSError?) ->作废如果让对象 = foundObject {var 追随者:PFObject = PFObject(className: "Followers")追随者[用户"] = 对象追随者[追随者"] = PFUser.currentUser()follower.saveEventually()}}}

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

解决方案

我以前遇到过这个问题,并通过在每个单元格中嵌入一个按钮来修复它.在您的 UITableView 中,您应该尝试使用 UIButton 嵌入每个单元格.

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

class MyCustomCell: UITableViewCell{@IBOutlet 弱变量 followButton:UIButton!var isFollowing:Bool = false//在这里声明其他单元格属性,如图片、名称、性别//......}

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

var myCellArray = [userListTableViewCell]()覆盖 func viewDidLoad(){super.viewDidLoad()var userQuery = PFUser.query()userQuery.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in如果让 usersArray = objects as![PF用户] {self.myCellArray.removeAll(keepCapacity: false)对于用户在 usersArray {如果让用户 = 对象为?PF用户{如果 user.objectId != PFUser.currentUser()?.objectId {var myCell = userListTableViewCell()myCell.userID = user.objectIdmyCell.username = user["fullName"] as!细绳myCell.gender = user["gender"] as!细绳var userPicture = user["profilePicure"] as?PF文件var image = UIImage(data:userPicture!.getData()!)myCell.displayPicture.image = 图像myCellArray.append(myCell)self.tableView.reloadData()}}}}})}覆盖 func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {myCellArray.count}覆盖 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {var cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier") as!userListTableViewCell//编辑每个单元格的故事板标签:cell.username.text = myCellArray[indexPath.row].username//等等....//为每个单元格嵌入一个按钮cell.followButton.layer.setValue(indexPath.row, forKey: "index")cell.followButton.addTarget(self, action: "followButtonTapped:", for ControlEvents: UIControlEvents.TouchUpInside)如果(myCellArray[indexPath.row].isFollowing == false){cell.followButton.setTitle("Follow", forState: .Normal)}别的{cell.followButton.setTitle("取消关注", forState: .Normal)}返回单元格}func followButtonTapped(sender: UIButton){让 cellIndex : Int = (sender.layer.valueForKey("index")) as!整数//您现在拥有按下播放按钮的单元格的索引,因此您可以执行以下操作if (myCellArray[cellIndex].isFollowing == false){myCellArray[cellIndex] = true}别的{myCellArray[cellIndex] = false}self.tableView.reloadData()}

I am retrieving data from "_User" class this way:

my declarations ..

 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

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??

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()

        }
    }
}

I am using sender.tag for the follow button here..

解决方案

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.

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
    // ......
}

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 检索对象时无法正确重新加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆