2014-09-10 3 views
0

앱이 시작되면 마지막 요소의 테이블 행을 선택해야합니다. plist에 마지막으로 사용한 요소의 이름입니다. 내가 사용하는 코드입니다 : 내가 발견 한 문제가선택 테이블 행이 작동하지 않는 경우 요소가 보이지 않는 경우

id plist = [[PlistHandler alloc] init];// FIXME: Dont work with long list, if the element is out of view. 
    if ([tableData containsObject:[plist readDataForKey:@"selectedFile"]]) { 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[tableData indexOfObject:[plist readDataForKey:@"selectedFile"]] inSection:0]; 
     [self.fileTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; 
     [self tableView:self.fileTable didSelectRowAtIndexPath:indexPath]; 
    } 

: 행이보기를 벗어나면 (테이블이 크고 요소의 경우보기 다음 이하), 나는 몇 가지 예외가 : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: selectedFile)' 어쩌면 누군가가 알고 행을 선택하거나 해결할 수있는 더 좋은 방법입니다.

업데이트 : 나는 조금을 디버그하는 지금은 문제가 발생 어디에 있는지 다음 cellText은 어쩌면 인덱스가 잘못, 전무하기 때문에

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString *cellText = cell.textLabel.text; 
    id plist = [[PlistHandler alloc] init]; 
    [plist writeDataToKey:@"path" andData:[NSString stringWithFormat:@"%@/%@", documentsDirectory, cellText]]; 
    [plist writeDataToKey:@"selectedFile" andData:cellText]; 
} 

그것은, 마지막 줄에 있습니다. 보기에있는 요소와 함께 작동하는 이유를 물어 봅니다.

답변

0

셀이 사용되지 않았기 때문에 아마 초기화되지 않았습니다. 화면에 없기 때문입니다.

더 심각한 문제는 UI에서 데이터를 가져 오는 것입니다. UI는 데이터를 표시하고 데이터 저장 및 검색을위한 것이 아닙니다. 모델에서 데이터를 검색하십시오. 응용 프로그램은 셀을 설정하는 방법을 알고 있기 때문에이를 수행하는 방법을 알고 있어야합니다.

https://developer.apple.com/library/mac/documentation/general/conceptual/devpedia-cocoacore/MVC.html