0

我有一个自定义 UITableView 单元。它包含一个带有图像的自定义按钮,该按钮显示一个复选框,可以点击该复选框以显示选中和未选中的图像。它可以正常工作,但是一旦开始使用 UISearchBarController 在单元格中点击我的自定义按钮进行过滤,图像就不会正确更新。结果是否显示在不同的表格视图中?更新单元格的方法都被调用了,但是图像没有改变。

更令人困惑的是:将单元格滚动出视图并向后滚动会导致单元格正确显示。

#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];
}
4

2 回答 2

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];
}
于 2013-06-22T18:14:01.413 回答
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];
}
于 2013-06-23T02:01:41.920 回答