0

I am using custom cell for my table view. In my cell , I have imageview & button. I am displaying 3 images in single row. When I select button , I am using checkbox image & when again tap on button , it deselect the checkbox image.When I select the button in first row & scroll the tabelview , it also checks the button on 3rd or 4th row. I think this is due to REUSE of cell. Here is my code for cellForRowAtIndexPath:

 - (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"LibraryElementsCell";
    LibraryElementsCell *cell = (LibraryElementsCell *) [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        NSArray *cellArray=[[NSBundle mainBundle] loadNibNamed:@"LibraryElementsCell" owner:self options:nil];
        cell=[cellArray objectAtIndex:0];

        [cell.firstElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
        [cell.firstElementButton addTarget:self action:@selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];

        [cell.secondElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
        [cell.secondElementButton addTarget:self action:@selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];

        [cell.thirdElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
        [cell.thirdElementButton addTarget:self action:@selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];

        tableView1.backgroundColor = [UIColor clearColor];
        tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NSMutableArray *tempArray = [self.categoriesArray objectAtIndex:indexPath.section];

    int row = indexPath.row * 3;

    if (row <= [tempArray count]) 
    {
        LibraryElement *libElement = [tempArray objectAtIndex:row];
        cell.firstElementImageView.image = [UIImage imageNamed:libElement.imageName];
    }

    if ((row + 1) < [tempArray count]) 
    {
        LibraryElement *libElement = [tempArray objectAtIndex:row+1];
        cell.secondElementImageView.image = [UIImage imageNamed:libElement.imageName];
    }
    else {
        [cell.secondElementImageView setHidden:YES];
        [cell.secondElementButton setHidden:YES];
    }

    if ((row + 2) < [tempArray count]) 
    {
        LibraryElement *libElement = [tempArray objectAtIndex:row+2];
        cell.thirdElementImageView.image = [UIImage imageNamed:libElement.imageName];
    }
    else {
        [cell.thirdElementImageView setHidden:YES];
        [cell.thirdElementButton setHidden:YES];
    }

    return cell;
}

Also some times hiding logic of button & imageview gets meessed up. It hides the imageview & button even if data is available.

Any knid of help is highly appreciated.Thanks

iOSAppDev
  • 2,755
  • 4
  • 39
  • 77

1 Answers1

0

You need to refresh the cell components each time e.g. create a refresh method inside the cell which takes your data and draw the cell components

refreshCellView:(NSDictionary)data

and inside cellForRowAtIndexPath call refreshCellView: after getting the cell

Mahmoud Adam
  • 5,772
  • 5
  • 41
  • 62