2
votes

Hi I'm hoping to get a custom cell accessory type to change between images. right now it is a checkmark and then no checkmark. I'm using Parse as my backend and each user has followers and is following multiple individuals. I'd like to change this to display between one of two images for following or +follow. Right now it's set for a checkmark. My current code is below for my did Does anyone have any suggestions?

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

    if cell.accessoryType == UITableViewCellAccessoryType.Checkmark {

        cell.accessoryType = UITableViewCellAccessoryType.None

        var query = PFQuery(className:"followers")
        query.whereKey("follower", equalTo:PFUser.currentUser().username)
        query.whereKey("following", equalTo:cell.textLabel?.text)
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error == nil {

                for object in objects {

                    object.delete()

                }
            } else {
                // Log details of the failure
                println(error)
            }
        }

    } else {

        cell.accessoryType = UITableViewCellAccessoryType.Checkmark

        var following = PFObject(className: "followers")
        following["following"] = cell.textLabel?.text
        following["follower"] = PFUser.currentUser().username

        following.save()

    }

}
1

1 Answers

1
votes

For me, I probably subclass a UIButton, and set the image for default state and selected state.

and then implement action:

    @IBAction func follow(sender: UIButton) {

        sender.selected = !sender.selected

        switch sender.selected {
        case true :
            print("do start follow")
        case false :
            print("cancel follow")
        }
    }

enter image description here