0

I am developing one app and i have one requirement that. I have to handle the Favourite button on custom cells like. i am creating the custom button with image on cell and setting unselected type Favourite image i am giving by default once user click on the Favourite button on cell i am changing the button image like selected favourite. I am using the following code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @“CustomCell”;
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.favButton.tag = indexPath.section;
    [cell.favButton addTarget:self action:@selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

Button Action:

-(void)handleFavouriteButton:(id)sender
{
    UIButton *button = sender;
    NSLog(@"selected favourite button tag %li", (long)button.tag);

    if (button.selected) {
        [button setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal];
    }
    else{
       [button setBackgroundImage:[UIImage imageNamed:@"favourites-Selected.png"] forState:UIControlStateNormal];
    }
    button.selected=!button.selected;
}

Using the above code i am able to change the change the Favourite button from normal to selected and selected to normal but problem is when i select the favourite button on first row it is changing 6 and 11 Ect.. rows also. Can anybody suggest me right way to do this

Thanks in advance.

nburk
  • 22,409
  • 18
  • 87
  • 132

2 Answers2

1

That button action and all things are looks fine. You need to save selected Button index as a tag in to the NSMutableArray like following example:

In.h Class:

interface myclass : UIViewController{
}
@property (strong, nonatomic) NSMutableArray *arrcontainstate;

In.m Class:

- (void)viewDidLoad {
    [super viewDidLoad];
    _arrcontainstate=[NSMutableArray array];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @“CustomCell”;
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.favButton.tag = indexPath.row;
            if ( [_arrcontainstate containsObject:indexPath.row]) {
                   [cell.favButton setBackgroundImage:[UIImage imageNamed:@"favourites-Selected.png"] forState:UIControlStateNormal];
            }
            else {
                [cell.favButton setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal];
            }

    [cell.favButton addTarget:self action:@selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)handleFavouriteButton:(id)sender
{
 UIButton *button = sender;
 button.selected=!button.selected;
    NSLog(@"selected favourite button tag %li", (long)button.tag);

    if (button.selected) {
         [_arrcontainstate addObject:button.tag];
         [button setBackgroundImage:[UIImage imageNamed:@"favourites-Selected.png"] forState:UIControlStateNormal];
    }
    else
    {
       [_arrcontainstate removeObject:button.tag];
       [button setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal];
    }
}
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • @nitin its working fine...but when we scrolls it changes states....so how to solve it – Bhavin Bhadani Apr 07 '15 at 10:07
  • if you notice and check cellForRowindex code i used "if ( [_arrcontainstate containsObject:indexPath.row]) {" that means if your idexPath and already contain in arraContainInstate then that you sould set status i mean set fevorites image – Nitin Gohel Apr 07 '15 at 10:10
0

As you are reusing the cell, the image of the button is not changing. First cell is reused in 6th and 11th cell so the button image remains selected. Use this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @“CustomCell”;
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.favButton.tag = indexPath.section;
    [cell.favButton setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal];
    [cell.favButton addTarget:self action:@selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • i am setting the image to button on storyboard. But once i click on favourite button in first row. Button image changing in first row, 6th and 11 th row as well – user1915706 Jan 28 '15 at 09:30
  • @user1915706 > Don't set image through storyboard. – Rashad Jan 28 '15 at 10:51