Reputation: 611
Having issues updating uibutton in specific uitableviewcell. When I change the button color it updates every button in uitableview. Attached my code below:
PFObject *post = [self.objectArray objectAtIndex:indexPath.section];
cell.likeBtn.layer.cornerRadius = 5.0f;
//To access button methods
[cell.likeBtn setTag:indexPath.section];
NSMutableDictionary *attributes = [NSMutableDictionary
dictionaryWithDictionary:[[TMMemoryCache sharedCache] objectForKey:post.objectId]];
BOOL likedByCurrentUser = [[attributes objectForKey:@"likedByCurrentUser"] boolValue];
if (likedByCurrentUser) {
cell.likeBtn.backgroundColor = [UIColor flatRedColor];
[cell.likeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cell.likeBtn setTitle:@"Liked" forState:UIControlStateNormal];
}else{
//NSLog(@"Did not like %@", post.objectId);
cell.likeBtn.backgroundColor = [UIColor flatBlueColor];
[cell.likeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cell.likeBtn setTitle:@"Like" forState:UIControlStateNormal];
}
is their a better way to update uibutton in just one specific cell?
Upvotes: 0
Views: 50
Reputation: 11598
There are two ways to do what you want, depending on how the updated information comes in:
cellForRowAtIndexPath:
.More often than not, #1 is the way to go. When new information comes into your UITableViewController
, do 1 of the following:
reloadData
to force a refresh of the entire table.reloadRowsAtIndexPaths:withRowAnimation:
passing in the correct index path values.In either of those scenarios, you'll set up the button correctly somewhere in your tableView:cellForRowAtIndexPath:
method. Generally, I create my UITableViewCell
subclasses to accept an object and configure themselves with the data in that object. That way, there's less code in my UITableViewController
that deals with cell configuration.
A typical implementation of tableView:cellForRowAtIndexPath:
looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"item" forIndexPath:indexPath];
[cell configureForItem:self.dataSource[indexPath.row]];
return cell;
}
Each of your UITableViewCell
instances will have their own button that will be configured by the data passed by the object in self.dataSource[indexPath.row]
. That way, as cells are recycled, the button isn't just passed around, but a new one is recycled and configured each time a new cell is needed.
Upvotes: 1
Reputation: 2875
At a guess, without seeing more of your code I would say your issue is with your use of indexPath.section instead of indexPath.row
If you have multiple buttons per section then they will all be covered by this code.
Upvotes: 0