筛选UITableView的内容 [英] filtering content for UITableView

查看:176
本文介绍了筛选UITableView的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看苹果的TableSearch示例代码。它看起来像他们有一个NSArray的所有内容,NSMutableArray过滤的内容。然后如果过滤器打开,那么他们会显示NSMutableArray。如果它关闭,他们会显示NSArray的所有数据。

1)我想知道这是否是一个通用的过滤器实现,因为我还没有做2)要添加到这个问题,如果我有一个四个不同类别的过滤器,我仍然会使用一个NSMutableArray,显示过滤内容时过滤器是否打开?或者,我为每个不同类型的过滤器创建四个不同的NSMutableArrays,然后根据所使用的过滤器显示该列表。假设通用实现是为列表创建一个NSArray,那么如果我要做的事情是创建过滤列表的数组是昂贵的,我会感到困惑四个不同的NSMutableArrays,或者如果取决于用户点击他们选择的过滤器选项,我应该动态创建NSMutableArray,然后重新加载 [tableView reloadData];



谢谢。

解决方案

在我面前的应用程序,但你通常会过滤使用谓词,所以这将有助于您审查关于NSPredicate的文档。所以当你想改变过滤器,你通过改变谓词来做到这一点。您不必创建所有过滤的结果。你只需要在任何时候创建一个你需要的。



使用数组,你可以使用类似于在本例中显示。关键字是

pre $ N $ credicate * predicate;
predicate = [NSPredicate predicateWithFormat:@length == 9];
NSArray * myArray2 = [myArray filteredArrayUsingPredicate:predicate];

过滤并不总是通过数组完成。如果使用核心数据,可以用 NSFetchedResultsControllers 完成。在那里也使用谓词,非常相似。谓词还可以用于其他事物,包括正则表达式过滤。这是值得一看的,如果你不熟悉它。


I was looking at the TableSearch example code from Apple. It looks like that they have a NSArray for all the content, and a NSMutableArray for filtered content. And then if the filter is on, then they would show the NSMutableArray. If it is off, they would show the NSArray that has all the data.

1) I was wondering if this is a common implementation for filters since I haven't done much filtering before.

2) To add to that question, if I had a filter of four different categories, would I still use one NSMutableArray that shows the filtered content when the filter is on? Or do I create four different NSMutableArrays for each different type of filter, and then show that list depending on which filter is on.

Assuming that the common implementation is to have an NSArray for the list, I'm getting confused if creating the arrays of filtered list up front is expensive if I were to do four different NSMutableArrays, or if depending on the click from the user of what filter option they select, should I create the NSMutableArray on the fly, and then reload the [tableView reloadData];

Thanks.

解决方案

I don't have that sample app in front of me, but you typically would filter using a predicate, so it would be helpful for you to review the docs on NSPredicate.

So when you want to change the filter, you do so by changing the predicate. You don't have to create all filtered results. You only create the one you need at any given moment.

With arrays, you can filter using code like that shown in this example. The key lines are

NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:@"length == 9"];
NSArray *myArray2 = [myArray filteredArrayUsingPredicate:predicate];

Filtering is not always done with arrays. It can be done with NSFetchedResultsControllers if using Core Data. Predicates are used there also, in very much the same way. Predicates can be used for other things, too, including regular expression filtering. It's worth looking at, if you aren't familiar with it.

这篇关于筛选UITableView的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆