UISearchBar搜索带有文本,子文本和图像的表格行 [英] UISearchBar Search table row with text, subtext and image

查看:93
本文介绍了UISearchBar搜索带有文本,子文本和图像的表格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,为了搜索表格,必须将该数据的副本插入到搜索数组中。

I've noticed that in order to do a search of a table, a copy of that data must be inserted to a search array.

例如。

    //Initialize the array.
listOfItems = [[NSMutableArray alloc] init];

NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Iceland", @"Greenland", @"Switzerland", @"Norway", @"New Zealand", @"Greece", @"Rome", @"Ireland", nil];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];

NSArray *countriesLivedInArray = [NSArray arrayWithObjects:@"India", @"U.S.A", nil];
NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:@"Countries"];

[listOfItems addObject:countriesToLiveInDict];
[listOfItems addObject:countriesLivedInDict];

//Initialize the copy array.
copyListOfItems = [[NSMutableArray alloc] init];

因此搜索的是存储在复制数组中的对象。

So what is searched is the objects that are stored in the copied array.

我的问题是,如何在特定单元格中搜索包含文本,子文本和图像的单元格行。

My Question is, how do I search Cell rows with text, subtext and image in that particular cell.

推荐答案

(1)

实际上没有任何搜索表格的东西。当用户在UISearchBar中输入文本时会发生什么事情完全取决于您 - 您可以将该操作视为您喜欢的任何内容。您所要做的就是充当结果表的委托和数据源,并形成结果表以响应构成任何表格基础的标准三大问题(您有多少部分?有多少部分?这一行中的行?这行的单元格是什么?)你喜欢的任何方式。结果表通常看起来像原始表的简化版本,但这根本不是必需的!它可以是您想要的任何表格。

There isn't really any such thing as searching a table. What happens when the user enters text in a UISearchBar is totally up to you - you can make that operation mean anything you like. All you have to do is function as the delegate-and-data-source for the results table and form the results table in response to the standard Three Big Questions that form the basis for any table ("how many sections have you? how many rows in this section? what's the cell for this row?") in any way you like. The results table does often look like a reduced version of the original table, but this is not at all required! It can be any table you want it to be.

(2)

不要混淆模型与视图。该表只是一个视图。您的数据是模型。您将要搜索的是Model,您的数据是原始表的基础。因此,当用户键入您的UISearchBar并开始搜索时,您希望形成一个新模型,该模型将成为结果表的基础。你如何形成它完全取决于你。通常,您需要过滤原始模型,以便结果模型中剩下的唯一内容是计为有效结果的内容。您可以通过遍历整个原始模型,将匹配搜索标准的所有内容放入新模型中来完成此操作。或者,如果原始模型是一个数组,您可以使用其中一个filteredArray方法来帮助您。最灵活的方法是使用块形成谓词,如本书中的示例所示:

Don't confuse Model with View. The table is just a view. Your data is Model. It is the Model, your data that is the basis of the original table, that you are going to be searching. So when the user types in your UISearchBar and you start searching, you want to form a new Model that will be the basis of the results table. How you form it is completely up to you. Typically you'll want to filter the original model so that the only stuff left in your results model is stuff that counts as a valid result. You could do this by walking the whole original model, putting everything that matches the search criterial into the new model. Or, if the original model is an array, you could use one of the filteredArray methods to help you. The most flexible way is to form a predicate with a block, as in this example from my book:

NSPredicate* p = [NSPredicate predicateWithBlock:
                  ^BOOL(id obj, NSDictionary *d) {
                      NSString* s = obj;
                      NSStringCompareOptions options = NSCaseInsensitiveSearch;
                      return ([s rangeOfString:sbc.searchBar.text 
                                       options:options].location != NSNotFound);
                  }];
self.filteredStates = [states filteredArrayUsingPredicate:p];

在该示例中, s (一项(数组)每次都是一个字符串,我想查看用户的搜索词是否出现在该字符串中。但如果您有一个字典或其他结构同时包含标题和副标题以及有关图像的信息,您可以以任何您喜欢的方式检查该字典。根据这个数组项是否通过基于搜索项的测试返回YES或NO,只要附加到传递测试的概念。

In that example, s (one item of the array) is a string each time, and I'm looking to see whether the user's search term occurs in that string. But if you had a dictionary or other structure holding both a title and a subtitle and info about an image, you could examine that dictionary in any way you like. It's just a matter of returning YES or NO according to whether this array item passes the test based on the search term, on whatever definition you attach to the notion of passing the test.

(3)

剩下的大问题是形成结果时模型。我通常首先使结果模型与原始模型相同,以响应 searchDisplayControllerWillBeginSearch ,因为否则结果表将在用户输入时显示无结果。 (这可能就是为什么你认为要做的第一件事就是复制原始模型。)然后,我可以做实际的过滤以响应 searchBarSearchButtonClicked (用户完成了)打字并点击了搜索),或者如果模型足够小,我可以在用户输入的每个字母后重新过滤它,以响应 searchBar:textDidChange (用户有在搜索栏中输入一个字母)。

The big question remaining is when to form the results model. I usually start by making the results model identical to the original model in response to searchDisplayControllerWillBeginSearch, because otherwise the results table will say No Results while the user is typing. (That is probably why you think the first thing to do is copy the original model.) Then, I can either do the actual filtering in response to searchBarSearchButtonClicked (the user is done typing and has tapped Search), or if the model is small enough, I can filter it afresh after every letter the user types, in response to searchBar:textDidChange (the user has typed a letter in the search bar).

这篇关于UISearchBar搜索带有文本,子文本和图像的表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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