2

I have a custom tableview cell that displays a Parse users profile image, username label and a button to add them as a "following" object within my Followers class. My relation setup is set as the "follower" within my "Followers" class as the username of the current user and the "following" as the username of the tableview cell that the "add friend" button was clicked. The way I am currently trying to achieve this is add a tag to the button that is set to indexPath.row and then adding a target and IBAction. From there I set the label view with a tag value of 1 and then try to set that label as a usernameLabel variable which I can use to set to the "following" column. Unfortunately my current solution, following["following"] = usernameLabel.text as PFObject gives me 'UIView? is not convertible to 'PFObject'. Is this the best way to achieve what I'm looking to do and any idea why this error is happening?

Here is my tableview cell:

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
    }



}

Here is my tableview functionality. (Note that this is a view controller with a tableview element):

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 = sender.superview?.viewWithTag(1)

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

        following.saveInBackground()


       /* if let surtv = tableView.objectAtIndex(sender.tag) as? SearchUsersRegistrationTableViewCell {

            surtv.usernameLabel = 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 Answer 1

1

You shouldn't really be using tags to try to navigate around like this. I know it's tempting, but it's cheating and leads to this kind of problem. It's also encouraging you to use your view as data storage which is also wrong.

You already have your custom cell class SearchUsersRegistrationTableViewCell, so you should leverage that. Give it a property to hold the individualUser. Now at least you can get the user back from the cell when the button is tapped.

Better is to have the cell update the user when the button is tapped, so you don't need to call back to the controller at all.

Better still is to create a custom class which holds the individualUser and the logic to apply the new follower and give that to the custom cell, then the cell asks that object for the individualUser info and tells that object when to add the follower. This is a better separation of knowledge and logic.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.