2014-04-13 2 views
1

에 머물 : 나는 사용자의 경우 선택한 행에 머물 체크 표시를 얻는 방법UITableViewCellAccessoryCheckmark 내가 행 선택에 당신이 체크 표시는 다음 코드를 사용하여 표시 할 수 있음을 알고있는 tableView

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
cell.accessoryType = UITableViewCellAccessoryCheckmark; 

을 서로 다른보기 컨트롤러간에 너무 이동합니까?

감사 UITableViewControllerNOclearsSelectionOnViewWillAppear를 설정하여에서

+0

특정 셀의 인덱스 경로를 저장해야합니다. – limon

답변

0

.

테이블보기 컨트롤러가 할당 해제 된 후 선택 사항을 유지하려면 선택한 항목을 NSUserDefaults과 같은 곳에 저장해야합니다. synchronize

편집의 사용에

-(void)viewDidLoad 
{  
    [super viewDidLoad]; 
    NSArray *previousSelection = [[NSUserDefaults standardDefaults] objectForKey:@"selection"]; 
    for (NSArray *selection in previousSelection) { 
     [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:selection[1] inSection:selection[0]] animated:NO scrollPosition:UITableViewScrollPositionNone]; 
    } 
} 

-(void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
    NSArray *selections = [self.tableView indexPathsForSelectedRows]; 
    NSMutableArray *selectionsToSave = [NSMutableArray array]; 
    for (NSIndexPath *selection in selections) { 
     [selectionsToSave addObject:@[selection.section, selection.row]]; 
    } 
    [[NSUserDefaults standardDefaults] setObject:selectionsToSave forKey:@"selection"]; 
    // Save as iOS 7 now saves less frequently - see note 
    [[NSUserDefaults standardDefaults] synchronize]; 
} 

참고 : 수정 답변 감사 만 NSUserDefaults 간단한 유형을 넣을 수 있다는 지적 @rmaddy합니다.

+0

이 방법은 작동하지 않습니다. 'NSUserDefaults'에'NSIndexPath' 객체를 저장할 수 없습니다. – rmaddy

+0

예! 당신 말이 맞아요, 제 대답을 바로 잡을 겁니다. – Rich

+0

@rmaddy가 더 좋습니다! 그 자리에 주셔서 감사합니다 :) – Rich

관련 문제