所以我现在正在收听来自searchBar的文本更改:
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText
{
[self filterContentForSearchText:searchText];
}
我想设计一个方法filterContentForsearchText
,以便在我输入时自动过滤我的UITableView。
我遇到的问题是我的UITableView很复杂。它有多个部分,包含多个字段。它基本上是一个联系人/地址簿,它是一个包含联系对象的数组数组。
CustomContact *con = [contacts[section_of_contact] allValues][0][x]
其中x是该部分中的特定行,返回包含con.fullName
等属性的CustomContact“con”。
UITableView目前在不同的部分显示联系人的完整名称。当我使用UISearchBar
输入时,如何过滤数组/ UITableView的这种结构?
以下是填写表格的方式:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BrowseContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BROWSECELL"];
CustomContact *thisContact = [self.contacts[indexPath.section] allValues][0][indexPath.row];
cell.labelName.text = thisContact.fullName;
return cell;
}
答案 0 :(得分:4)
我还创建了一个带有部分(数组数组)的地址簿,所以我只是发布我的解决方案,但这不是我自己的解决方案(我在某处在stackoverflow上找到了它):/ p>
在您的UITableViewController
子类中,只需添加以下两个UISearchDisplayDelegate
方法(link to the api reference)和“自定义”方法filterContentForSearchText
即可过滤您的数组。为了更好地理解,请阅读我在代码块中的注释。随时欢迎提出进一步的问题或想法。
#pragma mark - search Display Controller Delegate
- (BOOL) searchDisplayController : (UISearchDisplayController *) controller
shouldReloadTableForSearchString : (NSString *) searchString {
[self filterContentForSearchText : searchString
scope : [[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex : [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
#pragma mark - Search Filter
- (void) filterContentForSearchText : (NSString*) searchText
scope : (NSString*) scope {
// Here, instead of "lastName" or "firstName" just type your "fullName" a.s.o.
// I have also a custom NSObject subclass like your CustomContact that holds
//these strings like con.firstName or con.lastName
NSPredicate* resultPredicate = [NSPredicate predicateWithFormat : @" (lastName beginswith %@) OR (firstName beginsWith %@)", searchText, searchText];
// For this method, you just don't need to take your partitioned and
// somehow complicated contacts array (the array of arrays).
// Instead, just take an array that holds all your CustomContacts (unsorted):
NSArray* contactsArray = // here, get your unsorted contacts array from your data base or somewhere else you have stored your contacts;
// _searchResults is a private NSArray property, declared in this .m-file
// you will need this later (see below) in two of your UITableViewDataSource-methods:
_searchResults = [contactsArray filteredArrayUsingPredicate : resultPredicate];
// this is just for testing, if your search-Filter is working properly.
// Just remove it if you don't need it anymore
if (_searchResults.count == 0) {
NSLog(@" -> No Results (If that is wrong: try using simulator keyboard for testing!)");
} else {
NSLog(@" -> Number of search Results: %d", searchResults.count);
}
}
现在,您需要对以下三个UITableViewDataSource
- 方法进行一些更改:
注意:searchResultsTableView也加载了普通的公共数据源 通常被称为填充您的(分段地址簿)表格的方法:
<强> 1 强>
- (NSInteger) numberOfSectionsInTableView : (UITableView *) tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
// in our searchTableView we don't need to show the sections with headers
return 1;
}
else {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
}
}
<强> 2 强>
- (NSInteger) tableView : (UITableView *) tableView
numberOfRowsInSection : (NSInteger) section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
// return number of search results
return [_searchResults count];
} else {
// return count of the array that belongs to that section
// here, put (or just let it there like before) your
// [contacts[section] allValues][count]
return [[currentMNFaces objectAtIndex : section] count];
}
}
3。
- (UITableViewCell*) tableView : (UITableView *) tableView
cellForRowAtIndexPath : (NSIndexPath *) indexPath {
BrowseContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BROWSECELL"];
CustomContact *thisContact = nil;
// after loading your (custom) UITableViewCell
// you probably might load your object that will be put into the
// labels of that tableCell.
// This is the point, where you need to take care if this is your
// address-book tableView that is loaded, or your searchTable:
// load object...
if (tableView == self.searchDisplayController.searchResultsTableView) {
// from search list
faceAtIndex = [self->searchResults objectAtIndex: indexPath.row];
} else {
// from your contacts list
contactAtIndex = [self.contacts[indexPath.section] allValues][0][indexPath.row];
}
}
希望对你有用。