user3840618
user3840618

Reputation: 63

When I scroll the UITableViewCell ,before selected images changing in iOS

I need to implement this functionality.please help me. After "unblockbtnClick" click change the image ,it's fine ,but when scroll the tableview the selected images all are changing to previous images.

Here is my code.

  // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {


        static NSString *cellIdentifier = @"BlockedCell";


        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if(cell == nil)
        {
            NSArray *nib;
                nib = [[NSBundle mainBundle] loadNibNamed:@"BlockedCell" owner:self options:nil];
             cell = [nib objectAtIndex:0];


        }
        [btn_tblvcAddbtn addTarget:self action:@selector(unblockbtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn_tblvcAddbtn setTag:indexPath.row];
            return cell;
    }

    -(IBAction)unblockbtnClick:(UIButton *)sender{

        NSLog(@"Value of selected button = %ld",(long)[sender tag]);
        [sender setBackgroundImage:[UIImage imageNamed:@"remove.png"] forState:UIControlStateNormal];
    }

Upvotes: 0

Views: 700

Answers (3)

blazejmar
blazejmar

Reputation: 1090

When you're scrolling a cell out of the screen and then back - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method is called. You're not setting the background there, this is why it gets reset.

To fix you need to store information about selected cell and check it in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and apply proper background accordingly.

Upvotes: 0

Rohit
Rohit

Reputation: 577

-(IBAction)unblockbtnClick:(id)sender
{
    UIButton *btn=(UIButton *)sender;
    UITableViewCell *cellll;
    if([[[UIDevice currentDevice] systemVersion] intValue]<6)
        cellll=(UITableViewCell *)[btn superview];
    else
        cellll=(UITableViewCell *)[[btn superview]superview];



    NSIndexPath *aaa=[tblOption indexPathForCell:cellll];


    if([ArrSelectedData containsObject:[TblDataArrr objectAtIndex:aaa.row]])
    {
//tblDataArr is table dataArray  && ArrSelectedData is a array which contain selected itme of table 

        [ArrSelectedData removeObject:[TblDataArrr objectAtIndex:aaa.row]];
        [btn setBackgroundImage:[UIImage imageNamed:@"blank_checkbox.png"] forState:UIControlStateNormal];
    }
    else
    {
      [ArrSelectedData addObject:[TblDataArrr objectAtIndex:aaa.row]];
        [btn setBackgroundImage:[UIImage imageNamed:@"tick_checkbox.png"] forState:UIControlStateNormal];
    }

}

and this in cellForRowAtIndexPath

{
 if([ArrSelectedData containsObject:[TblDataArrr objectAtIndex:indexPath.row]])
          [tempBtn setBackgroundImage:[UIImage imageNamed:@"tick_checkbox.png"] forState:UIControlStateNormal];
    else
        [tempBtn setBackgroundImage:[UIImage imageNamed:@"blank_checkbox.png"] forState:UIControlStateNormal];

}

Upvotes: 0

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

You need to assign the nib file outside if condition

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        // initialize cell here
    }

   NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BlockedCell" owner:self options:nil];
   cell = [nib objectAtIndex:0];

Upvotes: 1

Related Questions