GOAT
GOAT

Reputation: 611

Change UIButton for Specific cell

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

Answers (2)

mbm29414
mbm29414

Reputation: 11598

There are two ways to do what you want, depending on how the updated information comes in:

  1. When configuring the cell in cellForRowAtIndexPath:.
  2. Let the cell manage its own contents.

More often than not, #1 is the way to go. When new information comes into your UITableViewController, do 1 of the following:

  1. Call reloadData to force a refresh of the entire table.
  2. Figure out which cells need to be updated and call 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

Gary Riches
Gary Riches

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

Related Questions