0

I have a custom UITableView Cell. It contains a custom button with image that shows a check box which can be tapped to show a checked and unchecked image. It works correctly, but once filtering begins using UISearchBarController tapping my custom button within a cell the image does not update properly. Are the results shown in perhaps a different tableview? The methods to update the cell are all called, but the image does not change.

What is more perplexing is: Scrolling the cell out of view and back causes the cell to be displayed correctly.

#define MAINLABEL_TAG 1

#define SECONDLABEL_TAG 2

#define CHECKBOX_TAG 3

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
static NSString *CellIdentifier = @"Cell";
UILabel *mainLabel, *secondLabel;
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
PDFFile *newPDF = [_pdfDocumentFiltered.pdfFileList objectAtIndex: indexPath.row];
//compute label sizes
GLfloat heightOfName = [TextSize computeHeightWithBoldFont: newPDF.documentTitle andViewWidth: 250 andFontSize: 18];
CGFloat heightOfDescription = [TextSize computeHeight: newPDF.description andViewWidth: 250 andFontSize: 16];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryNone;

    //set main label attributes
    mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0, 5.0, 250.0, heightOfName)];
    mainLabel.tag = MAINLABEL_TAG;
    mainLabel.font = [UIFont boldSystemFontOfSize: 18.0];
    mainLabel.textAlignment = UITextAlignmentLeft;
    mainLabel.textColor = [UIColor blackColor];
    mainLabel.autoresizingMask = UIViewAutoresizingNone;
    mainLabel.numberOfLines = 9;
    mainLabel.lineBreakMode = UILineBreakModeWordWrap;
    [cell.contentView addSubview:mainLabel];

    //set second label attributes
    secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0, heightOfName + 10, 250.0, heightOfDescription)];
    secondLabel.tag = SECONDLABEL_TAG;
    secondLabel.font = [UIFont systemFontOfSize:16.0];
    secondLabel.textAlignment = UITextAlignmentLeft;
    secondLabel.textColor = [UIColor darkGrayColor];
    secondLabel.autoresizingMask = UIViewAutoresizingNone;
    secondLabel.numberOfLines = 20;
    secondLabel.lineBreakMode = UILineBreakModeWordWrap;
    [cell.contentView addSubview:secondLabel];

    [button setFrame: CGRectMake(5.0, 0.0, 52.0, heightOfName + heightOfDescription + 15)];
    [button setTag: CHECKBOX_TAG];
    [button addTarget: self action: @selector(checkUncheck:) forControlEvents: UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];
} else {
    mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
    [mainLabel setFrame: CGRectMake(60.0, 0.0, 250.0, heightOfName)];

    secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
    [secondLabel setFrame: CGRectMake(60.0, heightOfName + 10, 250.0, heightOfDescription)];

    button = (UIButton *)[cell.contentView viewWithTag:CHECKBOX_TAG];
    [button setFrame: CGRectMake(5.0, 0.0, 52.0, heightOfName + heightOfDescription + 15)];
}
mainLabel.text = newPDF.documentTitle;
secondLabel.text = newPDF.description;

if ([newPDF.check boolValue] == YES)
{
    NSLog(@"Updating image to YES %i", indexPath.row);
    [button setImage: [UIImage imageNamed: @"ButtonCheck"] forState: UIControlStateNormal];
}else{
    NSLog(@"Updating Image to NO");
    [button setImage: [UIImage imageNamed: @"ButtonUncheck"] forState: UIControlStateNormal];
}
cell.tag = indexPath.row;
return cell;
}


- (IBAction) checkUncheck:(id)sender
{
UIButton *sendingButton = (UIButton *) sender;
UITableViewCell *cell =  (UITableViewCell *)[[sendingButton superview] superview];
PDFFile *newPDF = [_pdfDocumentFiltered.pdfFileList objectAtIndex: cell.tag];
[newPDF setCheck: [NSNumber numberWithBool: ![newPDF.check boolValue]]];
[self.table reloadData];
}
Joe Fratianni
  • 790
  • 8
  • 24

2 Answers2

0

You should not tag the cell but you should tag the button correctly.

[button setTag: indexPath.row]; //Tag it properly
 button = (UIButton *)[cell.contentView viewWithTag:indexPath.row]; //Fetch it properly

In IBAction,

- (IBAction) checkUncheck:(id)sender
{
UIButton *sendingButton = (UIButton *) sender;
PDFFile *newPDF = [_pdfDocumentFiltered.pdfFileList objectAtIndex: sendingButton.tag];
[newPDF setCheck: [NSNumber numberWithBool: ![newPDF.check boolValue]]];
[self.table reloadData];
}
Apurv
  • 17,116
  • 8
  • 51
  • 67
  • If I tag it with the indexpath.row how will I find it? button = (UIButton *)[cell.contentView viewWithTag:CHECKBOX_TAG]; – Joe Fratianni Jun 22 '13 at 19:12
0

Reloading the searchDisplayController is necessary. Not sure why but adding this line solves the problem:

    [self.searchDisplayController.searchResultsTableView reloadData];

So the updated method is the following:

- (IBAction) checkUncheck:(id)sender
{
UIButton *sendingButton = (UIButton *) sender;
UITableViewCell *cell =  (UITableViewCell *)[[sendingButton superview] superview];
PDFFile *newPDF = [_pdfDocumentFiltered.pdfFileList objectAtIndex: cell.tag];
[newPDF setCheck: [NSNumber numberWithBool: ![newPDF.check boolValue]]];
[self.table reloadData];
[self.searchDisplayController.searchResultsTableView reloadData];
}
Joe Fratianni
  • 790
  • 8
  • 24