我注意到,为了对表进行搜索,必须将该数据的副本插入到搜索数组中.
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)
不要将模型与视图混淆.该表只是一个视图.您的数据是模型.您将要搜索的是模型,即作为原始表基础的数据.因此,当用户在您的 UISearchBar 中键入并开始搜索时,您希望形成一个新的模型,它将作为结果表的基础.你如何形成它完全取决于你.通常,您需要过滤原始模型,以便结果模型中剩下的唯一内容是被视为有效结果的内容.您可以通过遍历整个原始模型来做到这一点,将符合搜索条件的所有内容放入新模型中.或者,如果原始模型是一个数组,您可以使用 filtersArray 方法之一来帮助您.最灵活的方法是用一个块来形成一个谓词,就像我书中的这个例子一样:
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
,否则结果表会在用户键入时显示 No Results.(这可能是您认为首先要做的是复制原始模型的原因.)然后,我可以进行实际过滤以响应 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 搜索带有文本、潜文本和图像的表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!