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

时间:2015-07-30 23:52:37

标签: ios swift uitableview parse-platform

我正在检索" _用户"这样上课:

我的声明..

 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作为关注按钮..

1 个答案:

答案 0 :(得分:1)

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

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

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