2013-06-22 47 views
0

我有一個自定義的UITableView單元格。它包含一個帶圖像的自定義按鈕,顯示一個複選框,可以點擊以顯示選中和未選中的圖像。它工作正常,但一旦過濾開始使用UISearchBarController點擊我的自定義按鈕在單元格內的圖像不正確更新。結果可能顯示在不同的tableview中嗎?更新單元格的方法都被調用,但圖像不會更改。無法訪問tableview單元格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]; 
} 

回答

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]; 
} 
+0

如果我與indexpath.row標記將如何找到它? button =(UIButton *)[cell.contentView viewWithTag:CHECKBOX_TAG]; –

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