据我所知,UISearchDisplayController在iOS 8.0中已被弃用,但是新的UISearchController并没有很多好的文档,所以我使用了前者。请耐心等待。
现在,我正在使用XIB文件。我知道对于常规的tableview,您可以通过进入XIB并从" Selection"下的下拉列表中选择Multiple Selection来允许多个单元格选择。
但是如何在UISearchBar的过滤搜索结果中实现这一点?据我所知,从技术上讲,我基本上有两个单独的表视图。
在这种情况下,我可以在常规表视图中使用多单元格选择(当我没有使用过滤器时)但是我不能在filter-tableview中这样做。我为常规tableview所做的只是允许多次选择"在XIB中。我不知道如何为filter-tableview这样做。
以下是构建我的tableview和搜索栏的所有相关代码。
#pragma mark Search Bar Methods
- (void)filterContentForSearchText:(NSString*)searchText scope: (NSString *) scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"firstName BEGINSWITH[c] %@", searchText];
self.searchResults = [[self.tbContactsGrabber.savedArrayOfContactsWithPhoneNumbers filteredArrayUsingPredicate:resultPredicate]mutableCopy];
}
- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles]objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
[tableView reloadData];
[self.tableView reloadData]; //these two lines make sure that both Filterview and Tableview data are refreshed - without it, it doesn't work
}
#pragma mark Tableview Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.searchResults count];
}
else {
return (self.tbContactsGrabber.savedArrayOfContactsWithPhoneNumbers.count);
}
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(!cell){
cell =
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
Contact *selectedContact;
if (tableView == self.searchDisplayController.searchResultsTableView){
//if we are in filter search results view
selectedContact = [self.searchResults objectAtIndex:indexPath.row];
if (selectedContact.checkmarkFlag == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else if (selectedContact.checkmarkFlag == NO) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
else {
//if we are in regular table view
selectedContact = [self.tbContactsGrabber.savedArrayOfContactsWithPhoneNumbers objectAtIndex:indexPath.row];
if (selectedContact.checkmarkFlag == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else if (selectedContact.checkmarkFlag == NO) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//to make sure there's no gray highlighting when it's clicked - important
NSString *fullName = [NSString stringWithFormat:@"%@ %@", selectedContact.firstName, selectedContact.lastName];
cell.textLabel.text = fullName;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Contact *selectedContact;
//if its filterview mode
if (tableView == self.searchDisplayController.searchResultsTableView){
selectedContact = [self.searchResults objectAtIndex:indexPath.row];
if (selectedContact.checkmarkFlag == YES) {
selectedContact.checkmarkFlag = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedContacts removeObject:selectedContact];
}
else {
selectedContact.checkmarkFlag = YES;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedContacts addObject:selectedContact];
}
}
//if its just regular tableview mode, and you selected something
else {
selectedContact = [self.tbContactsGrabber.savedArrayOfContactsWithPhoneNumbers objectAtIndex:indexPath.row];
selectedContact.checkmarkFlag = YES;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedContacts addObject:selectedContact];
}
NSLog(self.selectedContacts.description);
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Contact *selectedContact;
selectedContact = [self.tbContactsGrabber.savedArrayOfContactsWithPhoneNumbers objectAtIndex:indexPath.row];
selectedContact.checkmarkFlag = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedContacts removeObject:selectedContact];
NSLog(self.selectedContacts.description);
}
答案 0 :(得分:3)
self.searchDisplayController.searchResultsTableView.allowsMultipleSelection = YES;
现在,您的过滤器表视图允许多个选择。
答案 1 :(得分:0)
我之前已经回答了UITableViewController with UISearchDisplayController multiple selection sync
我怀疑这个问题与上面提到的问题重复。