2010-02-04 2 views
0

사용자가 필터 문자열을 입력 할 때 UIPicker가 데이터를 필터링하는 간단한 필터 메커니즘을 작성하려고합니다. 나는 거의 일하고있다.iPhone SDK : 필터를 적용한 후 NSMutableArray 데이터 세트를 복원하는 방법은 무엇입니까?

이것은 내 필터 코드이며 작동합니다. 내 질문에, 일단 내가 데이터 소스에 필터를 적용하면, 어떻게 술어를 제거하고 원래의 데이터 세트를 복원 할 수 있습니까? 내 데이터 소스에 NSMutableArray를 사용하고 있습니다.

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'B'"]; 
[arrayCountryChoices filterUsingPredicate: predicate]; 
[pickerCountry reloadComponent:0]; 

감사합니다. 이로 편집

는 방법 나는이 오류를 얻고을 제안했다. * 캐치되지 않은 예외 'NSInvalidArgumentException'으로 인해 앱 종료 중, 이유 : '* - [UIView numberOfComponentsInPickerView :] :

확실하지 않습니다. 이것은 내가 생각할 수있는 모든 관련 코드입니다. 나는 또한 IB에서 나의 연결을 점검했다. 왜 그런가?

//create data 
    arrayCountryChoices = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil]; 

    //copy the original array to searchable array 
    arraySearchResults = [arrayCountryChoices mutableCopy]; 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { 

    return 1; 
} 

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { 

    return [arraySearchResults count]; 

} 
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 

    return [arraySearchResults objectAtIndex:row]; 
} 
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 


    //grab the selected country 
    strUserChoice = [arraySearchResults objectAtIndex:row]; 

}

답변

1

왜 당신은 NSArray를 당신에게 원본 데이터를 보관하지 않습니다하고있는 NSMutableArray에 UIPickerView를 채우는 데이터? 응용 프로그램의 수명 동안 원래의 배열을 수정해야하는 경우


NSArray *myOriginalArray; 

/* 
Populate your array 
*/ 

NSMutableArray *myMutableArray = [myOriginalArray mutableCopy]; 


/* Apply your filters on the mutable array 
. 
. 
. 
. 
. 
*/ 


/* When you need to restore the data you simply restore you original array */ 
myMutableArray = [myOriginalArray mutableCopy]; 

, 당신은 변경 가능한 배열로 설정할 수 있습니다 당신은 원시 데이터를 할 때

그래서, 당신은 좋아합니까. 수정 된 배열을 이전 내용으로 복원해야 할 때마다 항상 기회를 제공합니다.

건배,
VFN

관련 문제