2013-06-22 10 views
0

사용자 지정 UITableView 셀이 있습니다. 체크 된 이미지와 체크되지 않은 이미지를 표시 할 수있는 체크 박스가 표시된 커스텀 버튼이 있습니다. 그것은 올바르게 작동하지만 일단 필터링이 UISearchBarController를 사용하여 셀 내에서 내 사용자 정의 단추를 누르면 이미지가 제대로 업데이트되지 않습니다. 결과가 다른 테이블 뷰에 표시됩니까? 셀을 업데이트하는 메서드는 모두 호출되지만 이미지는 변경되지 않습니다.테이블 뷰 셀에 액세스 할 수 없습니다. 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

셀에 태그를 지정하지 않아야하지만 버튼에 올바르게 태그를 지정해야합니다. searchDisplayController IBAction를 재로드에서

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

- (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

내가 그것을 어떻게 발견 할 것이다 :

[self.searchDisplayController.searchResultsTableView reloadData]; 

그래서 업데이트 방법은 다음과 같다 : 확실하지 왜 그러나이 줄을 추가하면 문제가 해결? button = (UIButton *) [cell.contentView viewWithTag : CHECKBOX_TAG]; –

0

필요하다. 나는 indexpath.row으로 태그를 추가하면

- (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]; 
} 
관련 문제