Swift向TableView单元添加按钮操作

时间:2015-05-01 13:57:38

标签: ios uitableview swift parse-platform

我试图在我的自定义tableview单元格中创建一个按钮,该按钮具有创建Parse类的操作(如果它尚未创建)并应用按钮单击的行的标签文本作为"以下"的值出现列并设置"关注者"列值作为当前登录用户的用户名。目前,我在addUser IBAction中的let usernameLabel处出现错误,我的类没有名为objectAtIndex的成员。实现我所寻找的目标的最佳途径是什么?

我在SearchUsersRegistrationTableViewCell.swift中创建了出口

import UIKit

class SearchUsersRegistrationTableViewCell: UITableViewCell {

    @IBOutlet var userImage: UIImageView!
    @IBOutlet 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
    }



}

以下是我的表格功能以及我尝试应用于addUserButton的操作:

import UIKit

class SearchUsersRegistrationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


    var userArray : NSMutableArray = []


    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        loadParseData()

        var user = PFUser.currentUser()




    }

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


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



    let textCellIdentifier = "Cell"

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.userArray.count
    }

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

        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, 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

        profileImage.getDataInBackgroundWithBlock({
            (result, error) in

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

        })

        cell.usernameLabel.text = username

        cell.addUserButton.tag = row

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

        return cell

    }


    @IBAction func addUser(sender: UIButton){

        let usernameLabel = self.objectAtIndex(sender.tag) as! String

        var following = PFObject(className: "Followers")
        following["following"] = usernameLabel.text
        following["follower"] = PFUser.currentUser().username //Currently logged in user

        following.saveInBackground()
    }

    @IBAction func finishAddingUsers(sender: AnyObject) {
        self.performSegueWithIdentifier("finishAddingUsers", sender: self)

    }

}

1 个答案:

答案 0 :(得分:2)

我假设usernameLabel与UIButton发件人位于同一contentView上。如果是这种情况,您可以将标签添加到usernameLabel并执行此操作

let usernameLabel = sender.superview.viewWithTag(/*Put tag of label here*/)

我在手机上,所以我不知道viewWithTag是否是该方法的正确名称,但它有类似之处。

希望这会有所帮助。