How do I select multiple table rows from filtered

2020-04-13 17:41发布

I understand that UISearchDisplayController got deprecated in iOS 8.0 but there's not a lot of good documentation around the new UISearchController so I used the former instead. Do bear with me.

Right now, I'm using XIB files. I know that for a regular tableview, you can allow multiple cell selection by going inside the XIB and and selecting Multiple Selection from the dropdown under "Selection".

But how can I make this possible in the filtered search results from a UISearchBar? I understand that technically, I have two separate tableviews basically.

In this scenario, I can use multi-cell selection in the regular tableview (when I'm not using the filter) but then I cannot do so in the filter-tableview. What I did for the regular tableview is just allow "multiple selection" in the XIB. I have no idea how to do so for the filter-tableview.

Below is all relevant code building out my tableview and searchbar.

    #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);
}

2条回答
贪生不怕死
2楼-- · 2020-04-13 18:25

I have answered this previously UITableViewController with UISearchDisplayController multiple selection sync

I suspect this question is a duplicate of the question linked above.

查看更多
我想做一个坏孩纸
3楼-- · 2020-04-13 18:30

Wow, turns out it's an incredibly easy solution. Just put this in viewDidLoad or wherever you please.

self.searchDisplayController.searchResultsTableView.allowsMultipleSelection = YES;

Now, your filter tableview allows multiple selection.

查看更多
登录 后发表回答