【发布时间】:2014-10-29 22:15:09
【问题描述】:
我知道 UISearchDisplayController 在 iOS 8.0 中已被弃用,但围绕新的 UISearchController 没有很多好的文档,所以我改用前者。请多多包涵。
现在,我正在使用 XIB 文件。我知道对于常规表格视图,您可以通过进入 XIB 并从“选择”下的下拉列表中选择多项选择来允许多个单元格选择。
但是我怎样才能在从 UISearchBar 过滤的搜索结果中实现这一点呢?我知道从技术上讲,我基本上有两个单独的表格视图。
在这种情况下,我可以在常规表格视图中使用多单元格选择(当我不使用过滤器时),但我不能在过滤器表格视图中这样做。我为常规表格视图所做的只是允许在 XIB 中“多选”。我不知道如何为 filter-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);
}
【问题讨论】:
标签: ios objective-c uitableview uisearchbar uisearchdisplaycontroller