NSArray
은 NSDictionary
입니다 (사전에는 몇 가지 키 값 쌍이 들어 있습니다). 내 이야기 게시판에는 여러 개의 UISegmentedControl
이 있으며 선택한 이벤트 중에 사전의 배열을 필터링합니다 (멋진 작품). 선택된 상태와 인스턴스 변수를 SegmentedControls에 저장하여 이전에 선택했는지 또는 처음 선택했는지 알 수 있습니다.여러 개의 UIControls를 사용하여 NSDictionaries의 NSArray를 필터링하십시오.
문제는 각 UISegmentControl
에 의해 생성 된 여러 조건자를 사용하여 사전 배열을 필터링하려고하는 것입니다. 예를 들어 SegmentControl 1을 클릭하면 그에 따라 배열이 필터링됩니다. SegmentControl 2를 클릭하면 현재 결과가 필터링됩니다. 그러나 SegmentControl 1에서 다른 값을 선택하면 모든 값이 원래 필터링되어 있기 때문에 아무것도 반환하지 않습니다. 이 세그먼트 중 5 개가 있으며 사실 viewDidLoad
에서 생성 된 필터링되지 않은 세트를 필터링 할 수 있어야합니다 (필터링되지 않은 데이터는 JSON 소스에서 가져오고 앱이로드 될 때 한 번 저장됩니다). JSON 소스에서 여러 번 가져 오는 것을 방지).
이 필터 문제를 해결하기 위해 몹시 괴롭 히고 있습니다. 나는 이것이 내 하루 종일을 보면서 내 머리가 으르렁 거리거나 objc (나는 많은 oop 프로그램 경험을 가지고있다. 그러나 이것은 나의 첫 번째 목표 C app이다)를 가진 나의 초보자 능력이라면 그것이 확실한 지 모른다. 나는 원래 필터링되지 않은 배열을 받아들이는 함수/메소드를 만들려고 생각했고 필터링 된 데이터를 반환하는 모든 선택된 필터 값을 생각했지만 일부 필터 필드가없는 경우 일어날 일을 머리로 감쌀 수 없었다. 이것을 접근하는 가장 좋은 방법은 무엇입니까? 여기에 몇 가지 코드가 있습니다 :
@interface ViewController()
{
//Checks for UISegments
Boolean isSegment1Selected;
Boolean isSegment2Selected;
Boolean isSegment3Selected;
Boolean isSegment4Selected;
//Instance FilterValues
NSString *segment1Value;
NSString *segment2Value;
NSString *segment3Value;
NSString *segment4Value;
//Create arrays for dictionaries
NSMutableArray *myArrayOfDictionaries;
NSMutableArray *unfilteredArrayOfDictionaries;
NSMutableArray *filteredArrayOfDictionaries;
}
@end
내 내 JSON 소스에서 데이터를 myArrayOfDictionaries
작성하고 있습니다 viewDidLoad()
.
필터링 할 때 사용하는 각 UISegmentView
에 대해 IBOutlet 이벤트가 있습니다. 내 UISegmentControl
의의
/// UISegmentedControl Filters ///
- (IBAction)segment1:(UISegmentedControl *)sender {
// NSLog(isSegment1Selected ? @"YES" : @"NO");
//Haven't used this if/else yet but guessing it will be part of the solution
if (segment1Value == nil)
{
NSLog(@"Nothing");
}
else
{
NSLog(@"Something");
}
//Grab value of SegmentControl into instance variable and strip additional characters not needed from the end
segment1Value = [[sender titleForSegmentAtIndex:sender.selectedSegmentIndex] substringToIndex:[sender titleForSegmentAtIndex:sender.selectedSegmentIndex].length -1 ];
//Create predicate filter with key and value
NSPredicate *predicateForSegment1 = [NSPredicate predicateWithFormat:@"(%K == %@)", segment1_key, segment1Value];
//Check if filteredArray already has objects
if ([filteredArrayOfDictionaries count] == 0)
{
//If filteredArray is empty, fill it with the filtered data using predicate from selected Segmented index control. Then clear the array and copy filtered array to filtered array
filteredArrayOfDictionaries = [[unfilteredArrayOfDictionaries filteredArrayUsingPredicate:predicateForSegment1] mutableCopy];
}
else
{
//If filtered array is full, empty it first then fill it with new filtered values.
NSArray *tmpArray = [[NSArray alloc] initWithArray:filteredArrayOfDictionaries];
[filteredArrayOfDictionaries removeAllObjects];
filteredArrayOfDictionaries = [[tmpArray filteredArrayUsingPredicate:predicateForImpellerDiameter] mutableCopy];
}
[myArrayOfDictionaries removeAllObjects];
[myArrayOfDictionaries addObjectsFromArray:filteredArrayOfDictionaries];
[tableData reloadData];
}
모든는 다음과 같습니다 : 그들은 모두 거의 같은 마이너스 이름 때문에 나는 당신에게 하나를 보여 드리겠습니다. 각 SegmentControl 이벤트를 호출하여 필터링 된 배열을 반환하는 함수를 작성하기 시작했으나 이것이 최선의 방법인지는 확실하지 않습니다. 여기에 내가 지금까지 가지고있는 것이있다. 나는 술어 또는 단지 문자열을 전달해야하는지 잘 모르겠다.
- (NSArray*)filteredResults:(NSArray*)unfilteredArray forSegment1:(NSPredicate*)segment1 forSegment2:(NSPredicate*)segment2 forSegment3:(NSPredicate*)segment3 forSegment4:(NSPredicate*)segment4;
나는 올바른 방향으로 가고 있습니까? 그것은 모두 정말로 혼란스러워졌습니다. Segment1을 선택한 다음 Segment2를 선택하면 올바르게 필터링된다는 것을 알고 있습니다. 그러나 이미 선택한 세그먼트의 값을 변경하는 즉시 세그먼트 값이 첫 번째 선택 항목에 의해 필터링 되었기 때문에 아무 것도 반환하지 않습니다. 이전에 세그먼트 컨트롤을 선택했을 때 필 터링 된 것과 필터링되지 않은 배열을 비교할 수 있습니까? 나는 이것이 정말로 합리적이되기를 바란다. 보고와 독서에 감사드립니다.
사용 해보십시오 : NSArray * tmpArray = [[NSArray alloc] initWithArray : unfilteredArrayOfDictionaries]; 대신에 : NSArray * tmpArray = [[NSArray alloc] initWithArray : filteredArrayOfDictionaries]; –
하지만 선택한 다른 세그먼트 컨트롤에서 필터링이 제거됩니다. 맞습니까? – drpcken
죄송합니다. 명확하게 알 수 없습니다 :( –