我有一个自定义的UITableView Cell。它包含一个带有图像的自定义按钮,该按钮显示一个复选框,可以点击该复选框以显示已选中和未选中的图像。它工作正常,但一旦过滤开始使用UISearchBarController在单元格内点击我的自定义按钮,图像就无法正确更新。结果是否显示在不同的tableview中?更新单元格的方法都被调用,但图像不会改变。
更令人困惑的是:将单元格滚出视图并向后滚动会导致单元格正确显示。
#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];
}
答案 0 :(得分:0)
您不应该标记单元格,但应该正确标记按钮。
[button setTag: indexPath.row]; //Tag it properly
button = (UIButton *)[cell.contentView viewWithTag:indexPath.row]; //Fetch it properly
在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];
}
答案 1 :(得分:0)
重新加载searchDisplayController是必要的。不确定为什么但添加此行可以解决问题:
[self.searchDisplayController.searchResultsTableView reloadData];
所以更新的方法如下:
- (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];
}