通过Ui Controls过滤核心数据记录的最佳实践

时间:2012-04-21 08:13:05

标签: ios core-data uibutton nspredicate

我正在构建一个应用程序,用户可以根据4个参数过滤一组照片:

model http://seismicdevelopment.com/shot.png

FeedType和PhotoType可以分别是三个可能值之一。市场和标签可以包含描述照片的多个值。我有ui设置FeedType / PhotoType是每个参数的一系列UISwitch。如果您打开一个值,该组中的其他值将关闭。

市场/标签具有可以选择/正常的UIButton。应将任何选定的按钮添加到过滤中以缩小结果范围。

我似乎无法弄清楚应该如何编写过滤逻辑以使其最有效。基本上在任何控件更改上,我需要重写谓词过滤器,但我似乎无法理解如何将单独的param过滤器链接在一起。

对于Tag / Market我还想为没有记录的UIButton添加禁用状态,或者根据当前选择的按钮添加没有记录的禁用状态。

1 个答案:

答案 0 :(得分:2)

在不知道参数是什么类型的情况下很难说出精确的建议,但是,据我所知,你需要有四个变量,更有可能是NSStrings,每次用户更改参数时,你通过使用正确的格式附加所有变量来创建新的fetch谓词。这就是我一般的看法。

更新

所以,使用Market和Tag Butons一切都很明显 - 你应该使用NSCompondPredicate。你需要一个谓词数组,所以要建立一个属性。

@property (strong) NSMutableArray *subpredicates;

然后,每次用户触摸按钮时,您都会添加或删除该缩小谓词。

- (void)tagButtonTouched:(UIButton *)button
{
    if (thisTagIsNotYetSelected)
        [subpredicates addObject:[NSPredicate predicateWithFormat:@"ANY tags.name == %@", button.titleLabel.text]];//or some other predicate based on that button title
    else
        [subpredicates removeObject:[NSPredicate predicateWithFormat:@"ANY tags.name == %@",button.titleLabel.text] ]];
}

Markets

也是如此
 - (void)marketButtonTouched:(UIButton *)button
 {
     if (thisMarketIsNotYetSelected)
         [subpredicates addObject:[NSPredicate predicateWithFormat:@"ANY markets.name == %@", button.titleLabel.text]];//or some other predicate based on that button title
     else
         [subpredicates removeObject:[NSPredicate predicateWithFormat:@"ANY markets.name == %@",button.titleLabel.text] ]];
 }

您还应该添加两个变量,我猜是NSPredicatesNSString,它们将保留所选FeedTypePhotoType的值,让我们称之为{{1分别是{}}和feedTypePredicate。当用户触摸那些开关时(为什么不是segmentedControls?),你只需更改它们的值。

最后,你应该将所有这些谓词合并为一个并进行获取!

photoTypePredicate