Я понимаю, что UISearchDisplayController устарел в iOS 8.0, но нет большого количества хорошей документации вокруг нового UISearchController, поэтому я использовал его вместо этого. Не медведь со мной.Как выбрать несколько строк таблицы из фильтрованных результатов панели поиска и SearchDisplayController?
Прямо сейчас, я использую файлы XIB. Я знаю, что для обычного просмотра таблицы вы можете разрешить выбор нескольких ячеек, перейдя внутрь XIB и выбрав «Множественный выбор» из раскрывающегося списка в разделе «Выбор».
Но как я могу сделать это возможным в отфильтрованных результатах поиска из UISearchBar? Я понимаю, что в техническом плане у меня есть два отдельных таблицы.
В этом случае я могу использовать многоэлементный выбор в регулярном представлении таблицы (когда я не использую фильтр), но тогда я не могу этого сделать в файле filter-tableview. То, что я сделал для обычного табличного представления, просто разрешает «множественный выбор» в XIB. Я не знаю, как это сделать для фильтра-таблицы.
Ниже приведен весь код, строящий мой стол и панель поиска.
#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);
}
спасибо, но этот вопрос кажется немного другим. Я уже получил часть «sync» для работы; например, если я выберу несколько строк из «обычного» табличного представления, все они будут выбраны с помощью галочки в таблице «filter». Но конкретная проблема, с которой я сталкиваюсь, в действительности имеет меньшую область действия ... Я просто хочу, чтобы иметь возможность выбирать несколько строк в таблице «фильтр», но я могу сделать это только в «обычном» представлении таблицы. –