【问题标题】:How do I select multiple table rows from filtered results of a Searchbar and SearchDisplayController?如何从搜索栏和 UISearchDisplayController 的过滤结果中选择多个表行?
【发布时间】: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


    【解决方案1】:

    哇,原来这是一个非常简单的解决方案。 只需将它放在 viewDidLoad 或任何你喜欢的地方。

    self.searchDisplayController.searchResultsTableView.allowsMultipleSelection = YES;
    

    现在,您的过滤表视图允许多选。

    【讨论】:

    • 在 iOS 8 上,我在 reloadData 之前设置了allowsMultipleSelection。 [此处代码] (pastie.org/10022152)
    【解决方案2】:

    我之前回答过这个问题UITableViewController with UISearchDisplayController multiple selection sync

    我怀疑这个问题与上面链接的问题重复。

    【讨论】:

    • 谢谢,但这个问题似乎有点不同。我已经完成了“同步”部分的工作;例如,如果我从“常规”表格视图中选择多行,它们都显示为选中,并在“过滤器”表格视图上带有复选标记。但我遇到的具体问题实际上范围更小......我只想能够在“过滤器”表视图中选择多行,但我只能在“常规”表视图中进行。
    猜你喜欢
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 2020-04-07
    • 2012-01-30
    • 1970-01-01
    • 2020-02-15
    相关资源
    最近更新 更多