0

I have a custom UITableViewCell which contains two UIButtons (An upVote button and a downVote button), and a label that is meant to count the number of votes. I am using the Parse framework for my backend.

I cannot figure out how to associate the value of a particular label with the custom UITableViewCell that contains the label so that I can then save it to the Parse backend. How can I reference the indexPath of the cell that contains the button using Swift and then associate it with the label so that I can then save it to Parse?

As always, if you feel there is a better way, please share.

enter image description here.

2 Answers 2

1

Everything can happen in your custom UITableViewCell. The key is to store the parse object as a property of the UITableViewCell in cellForRowAtIndexPath: so you never need to worry about looking up the indexPath. Hook your two UIButtons up and when a button is tapped: update the vote count on the parse object, update the label, save the parse object. Something like this should give you the idea:

@interface CustomTableViewCell

@property (nonatomic, strong) MyParseData *parseObject;
@property (nonatomic, strong) UILabel *voteCountLabel;

@end

@implementation CustomTableViewCell

- (IBAction)upVoteButton:(id)sender {
    self.parseObject.voteCount++;
    [self updateVote];
}

- (IBAction)downVoteButton:(id)sender {
    self.parseObject.voteCount--;
    [self updateVote];
}

- (void)updateVote {
    self.voteCountLabel.text = [self.parseObject.voteCount description];
    [self.parseObject saveInBackground];
}

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

1 Comment

Thank you so much for answering, but unfortunately I am using swift and am new to IOS. Could you please explain it in terms of swift. Thankyou
0

I think you created a custom cell to do this. So you have to create two methods in the custom cell class that will handle the touch up inside to its delegate (the tableview controller, for example). When delegate is called, it has a reference to the cell touched and you can go on.

2 Comments

So should I put the delegate method inside the IBAction of the buttons?
You have to declare a protocol in your cell interface file. Then, you have to call your delegate into the IBAction

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.