【发布时间】:2014-10-14 04:32:45
【问题描述】:
我有一个NSArray 的NSDictionarys(字典包含几个键值对)。在我的故事板上,我有几个UISegmentedControls,在他们选择的事件中,我过滤掉了我的字典数组(很好用)。我将这些 SegmentedControls 的选定状态和值存储在实例变量中,这样我就可以判断它们是以前被选中还是第一次被选中。
我遇到的问题是尝试使用每个UISegmentControl 创建的多个谓词过滤这个字典数组。例如,如果我单击 SegmentControl 1,它会相应地过滤数组,如果我单击 SegmentControl 2,它会获取当前结果并过滤这些结果。但是,如果我从 SegmentControl 1 中选择另一个值,它不会返回任何值,因为所有这些值最初都被过滤掉了。由于我有 5 个这些段,我需要确保它可以过滤在我的 viewDidLoad 上生成的未过滤集(我的未过滤数据来自 JSON 源,并且在应用程序时存储一次,这更加突出了这一点)加载以防止它多次从 JSON 源中提取)。
我在尝试解决这个过滤器问题时遇到了麻烦。我不确定是不是因为整天看这个我的大脑糊涂了,还是因为我对 objc 的新手能力(我有很多 oop 程序经验,但这是我的第一个目标 c 应用程序)。我最初考虑过制作一个接受未过滤数组的函数/方法,以及所有选择的过滤器值以返回过滤后的数据,但我无法理解如果某些过滤器字段为 nil 会发生什么。解决这个问题的最佳方法是什么?这是一些代码:
@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
在我的 viewDidLoad() 上,我正在使用来自我的 JSON 源的数据填充 myArrayOfDictionaries。
对于我用来过滤的每个UISegmentView,我都有 IBOutlet 事件。我给你看一个,因为除了名字之外它们几乎都一样:
/// 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];
}
我所有的UISegmentControls 都是这样的。我开始编写一个函数来调用每个 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];
-
但这会从任何其他选定的段控件中删除任何过滤,对吧?
-
抱歉我看不清楚:(
标签: ios objective-c nsdictionary nspredicate