我如何使用 NSPredicate 和 UISegmentedControl 过滤 UITableView

how do i filter UITableView using NSPredicate and UISegmentedControl

我做的第一件事是将 @property (strong, nonatomic) IBOutlet UISegmentedControl *statusControl; 添加到我的 .h 文件并在我的 .m 文件中合成它 然后我将以下代码链接到故事板上的段控制器以及我之前提到的 IBOutlet

-(IBAction)indexChanged:(UISegmentedControl *)sender {
    switch (statusControl.selectedSegmentIndex) {
        case 0:
            selectedStatus = @"ALL";


            NSLog(@"ALL");
            break;
        case 1:{
            selectedStatus = @"NEW";

            NSPredicate *new= [NSPredicate predicateWithFormat:@"SELF.STATUS contains[c] NEW"];
            listOfEstNetGrand = [[listOfEstNetGrand filteredArrayUsingPredicate:new] mutableCopy];

            NSLog(@"NEW");
            break;
        }
        case 2: {
            selectedStatus = @"COMPLETED";

            NSPredicate *completed = [NSPredicate predicateWithFormat:@"SELF.STATUS contains[c] COMPLETED"];
            listOfEstNetGrand = [[listOfEstNetGrand filteredArrayUsingPredicate:completed] mutableCopy];

            NSLog(@"COMPLETED");
            break;
        }

    }
    [self.tableView reloadData];
}

我 运行 调试,我在这里遇到了崩溃 listOfEstNetGrand = [[listOfEstNetGrand filteredArrayUsingPredicate:new] mutableCopy];

没有代码,我正在记录正确的 NSLog,所以我知道按钮可以工作,但我无法过滤我的 uitableview。我做错了什么?

如有任何帮助,我们将不胜感激。谢谢

编辑:这是我遇到的崩溃 *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CactusQueue 0x13ce87800> valueForUndefinedKey:]: this class is not key value coding-compliant for the key COMPLETED.'*** First throw call stack:

编辑 2:我将代码更改为 NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"STATUS contains[cd] COMPLETED"]; 并且有效。不知道为什么,因为我在搜索

时使用 SELF.

你的谓词应该像

NSPredicate *completed = [NSPredicate predicateWithFormat:@"SELF.STATUS contains[c] 'COMPLETED'"];

NSPredicate *completed = [NSPredicate predicateWithFormat:@"SELF.STATUS contains[c] %@",@"COMPLETED"];

请注意您提供的值 '。否则predicate会认为它是一个属性的对象