2011-08-05 4 views
0

카테고리가 1 행만 선택됩니다. 하지만로드 할 때 영혼 행을 선택하는 것이 좋습니다. 이 같은UITableview에서로드 한 후 행을 선택하고 확인하는 방법

뭔가 : 그것을 위해서는

enter image description here

나는이 (가)에 대한 비교 수행한다

enter image description here

순간은 아무것도 선택하지 않고이에 관해서 selectRowAtIndexPath?

// 표보기 셀의 모양을 사용자 정의하십시오.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 


    categoryString = [arrayCategory objectAtIndex:indexPath.row]; 
    cell.textLabel.text = categoryString; 
    if (cell.textLabel.text == categoryString) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    } 

    return cell; 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell* newCell = [tableView cellForRowAtIndexPath:indexPath]; 
    int newRow = [indexPath row]; 
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1; 

    if(newRow != oldRow) 
    { 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
     newCell.highlighted =YES; 
     UITableViewCell* oldCell = [tableView cellForRowAtIndexPath:lastIndexPath]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 
     lastIndexPath = indexPath; 
     oldCell.highlighted = NO; 

    } 
    [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES]; 
} 
+0

accessoryType – Eimantas

답변

1

프로그래밍 방식으로 행을 선택하려면 selectRowAtIndexPath : animated : scrollPosition : 메서드를 사용하십시오.

코드에서 수정해야 할 사항은 없습니다. 문자열 if ([cell.textLabel.text isEqualToString:categoryString]) {과 같이 문자열을 일치 시키려면 isEqualToString: 메서드를 사용해야합니다. 다음은 categoryStringcell.textLabel.text에 할당하고 다음 줄에 일치시키는 것이므로 항상 YES을 반환합니다. 나는 당신이 틀린 가치를 매치하고 있다고 생각한다.

+0

을 할당 한 후 데이터를 다시로드해야합니다. selectRowAtIndexPath : animated : scrollPosition :? – Desmond

+0

이 메소드는 테이블을 특정 행으로 이동하려는 모든 위치에서 호출 할 수 있습니다. – EmptyStack

+0

안녕하세요, 카테고리 문자열을 행과 일치 시켜서 올바른 행을 선택할 수 있습니다. 내가 어떻게 할 수 있는지 알지? 덕분에 샘플 코드가 있다면 좋을 것입니다. :) 이 코드를 실행하고 있지만 모든 행을 선택했습니다. if (cell.textLabel.text == categoryString) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } – Desmond

0

수동으로 여러 개의 체크 표시를 설정해야합니다. 각 UITableViewCell에 대해 UIImageview를 사용할 수 있으며 저장된 이전 데이터에 따라 didSelectRowAtIndexPath (UITableView의 대리자 메서드)에 체크 표시를 설정해야합니다.

관련 문제