2010-08-11 4 views
0

셀을 "확인"한 속성을 파일에서 selectedArray 속성으로 정의했습니다. 셀을 표시 할 때 현재 셀 값이 selectedArray에 있는지 여부를 확인하고 표시되어있는 경우 UITableViewCellAccessoryCheckmark를 테이블의 액세서리보기로 표시합니다.UITableView 적절하게 새로 고치지 않음

유일한 문제는 사용자가 UISearchBar를 사용하여 UITableView를 검색 할 때 검색을 완료하면 테이블에 reloadData를 요청하고 numberofRows 메서드에서는 selectedArray에 올바른 양의 개체가 표시됩니다. 그러나 cellForRowatIndexPath 메서드는 selectedArray가 업데이트 된 적이없는 것처럼 이전 체크 표시를 계속 표시합니다.

아무도 도와 줄 수 있습니까? 내 코드는 다음과 같습니다.

NSArray *tempArray = [[NSArray alloc] init]; 
if(tableView == theTable) { 
    tempArray = [contactsArray objectAtIndex:indexPath.row]; 
} else { 
    tempArray = [searchfor_array objectAtIndex:indexPath.row]; 
} 

NSString *cellValue = [tempArray objectAtIndex:0]; 
static NSString *CellIdentifier; 
CellIdentifier = cellValue; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(70, 30, 200, 20)]; 
    name.text = [tempArray objectAtIndex:0]; 
    name.backgroundColor = [UIColor clearColor]; 
    [name setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]]; 
    [cell.contentView addSubview:name]; 
    [name release]; 

    if([selectedArray containsObject:[tempArray objectAtIndex:0]]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

} 

return cell; 
[cell release]; 
[tempArray release]; 
[cellValue release]; 

답변

0

- 당신은 경우에 레이블을 추가해야

(셀 == 전무) 부분 만. 해당 명령문의 부속 뷰 OUTSIDE를 처리합니다. 여기 것처럼 보일 것입니다 무엇 - 그래도 난이 작업을 수행 할 때,이 문제가 해결되지 및 레이블이 서로 쌓아 시작

if (cell == nil) { 

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(70, 30, 200, 20)]; 
    name.text = [tempArray objectAtIndex:0]; 
    name.backgroundColor = [UIColor clearColor]; 
    [name setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]]; 
    [cell.contentView addSubview:name]; 
    [name release]; 


} 

if([selectedArray containsObject:[tempArray objectAtIndex:0]]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} else { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 
+0

내 답변에 공백을 채워 주셔서 감사합니다. – Justin

1

셀을 한 번 설정하고 다시 사용하는 것 같습니다. 코드를 단계별로 실행하면 셀이 처음로드 될 때만 if 블록을 입력해야합니다.

당신은, 각 행의 셀을 조정과 같이 할 수 있습니다 : 저스틴의 대답에 추가

if (cell == nil) { 
    // Initialize cell. 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Adjust cell for each row. 
if([selectedArray containsObject:[tempArray objectAtIndex:0]]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
+0

... –

+0

내가 자세히하지 않았다 설정하는 방법 셀과 액세서리보기를 지우려면 다른 사람을 놓쳤습니다. 라파엘이 다 알아 냈어. – Justin

관련 문제