迅速使用未解决的标识符' IndexPath' TableView单元格按钮

时间:2015-05-06 13:49:15

标签: ios uitableview swift parse-platform

我试图获取我的UILabel的文本并将其设置为我的Parse对象,但是我遇到了将对象设置为单元格的索引路径的问题。我在该行收到Use of unresolved identifier 'indexPath'错误。

follow["following"] = self.userArray.objectAtIndex(IndexPath.row)

这是我的tableview控制器:

import UIKit

class SearchUsersRegistrationTableViewController: UITableViewController {

    var userArray : NSMutableArray = []

    override func viewDidLoad() {
        super.viewDidLoad()

        var user = PFUser.currentUser()

        loadParseData()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return userArray.count

    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell: SearchUsersRegistrationTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SearchUsersRegistrationTableViewCell

        let row = indexPath.row

        var individualUser = userArray[row] as! PFUser
        var username = individualUser.username as String

        var profileImage = individualUser["profileImage"] as? PFFile

        if profileImage != nil {

        profileImage!.getDataInBackgroundWithBlock({
            (result, error) in

            cell.userImage.image = UIImage(data: result)

        })
        } else {
            cell.userImage.image = UIImage(named: "profileImagePlaceHolder")

        }

        cell.usernameLabel.text = username

        cell.addUserButton.tag = row

        cell.addUserButton.addTarget(self, action: "addUser:", forControlEvents: .TouchUpInside)

        return cell

    }

    func loadParseData() {

        var query : PFQuery = PFUser.query()

        query.findObjectsInBackgroundWithBlock {
            (objects:[AnyObject]!, error:NSError!) -> Void in

            if error == nil {

                if let objects = objects {

                    println("\(objects.count) users are listed")

                    for object in objects {

                        self.userArray.addObject(object)

                    }
                    self.tableView.reloadData()
                }
            } else {
                println("There is an error")
            }
        }
    }

    @IBAction func addUser(sender: UIButton) {

        println("Button Triggered")

        let addUserButton : UIButton = sender

        let user : PFObject = self.userArray.objectAtIndex(addUserButton.tag) as! PFObject



        var follow = PFObject(className: "Follow")


        follow["following"] = self.userArray.objectAtIndex(IndexPath.row)
        follow["follower"] = PFUser.currentUser().username


        follow.saveInBackground()

    }

}

这是我的tableview单元格:

import UIKit

class SearchUsersRegistrationTableViewCell: UITableViewCell {



    @IBOutlet weak var userImage: UIImageView!
    @IBOutlet weak var usernameLabel: UILabel!
    @IBOutlet weak var addUserButton: UIButton!


    override func awakeFromNib() {
        super.awakeFromNib()

        userImage.layer.borderWidth = 1
        userImage.layer.masksToBounds = false
        userImage.layer.borderColor = UIColor.whiteColor().CGColor
        userImage.layer.cornerRadius = userImage.frame.width/2
        userImage.clipsToBounds = true


    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }



}

2 个答案:

答案 0 :(得分:0)

试试这个

follow["following"] = self.userArray.objectAtIndex(sender.tag)

您正在将行设置为按钮的标记。只需使用它。

答案 1 :(得分:0)

在这种情况下你不应该使用标签。 要在addUser函数中获取索引路径,请添加以下内容:

let indexPath = tableView.indexPathForRowAtPoint(addUserButton.convertPoint(CGPointZero, toView: tableView))

然后你可以使用那一行:

follow["following"] = self.userArray.objectAtIndex(indexPath.row)

indexPath,而不是IndexPath