如何从Searchbar和SearchDisplayController的过滤结果中选择多个表行? [英] How do I select multiple table rows from filtered results of a Searchbar and SearchDisplayController?

查看:69
本文介绍了如何从Searchbar和SearchDisplayController的过滤结果中选择多个表行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道UISearchDisplayController在iOS 8.0中已弃用,但是关于新的UISearchController的文档并不多,所以我改用了前者.忍受我.

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.

现在,我正在使用XIB文件.我知道对于常规表格视图,您可以通过在XIB内并从选择"下的下拉列表中选择多个选择"来允许多个单元格选择.

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".

但是如何在UISearchBar的过滤搜索结果中实现这一点?我了解从技术上讲,基本上我有两个单独的表视图.

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.

在这种情况下,我可以在常规表视图中使用多单元格选择(当我不使用过滤器时),但是在过滤器表视图中则不能这样做.我对常规表格视图所做的只是在XIB中允许多重选择".我不知道如何为filter-tableview这样做.

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

推荐答案

哇,事实证明这是一个非常简单的解决方案. 只需将其放在viewDidLoad或您喜欢的任何位置.

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.

这篇关于如何从Searchbar和SearchDisplayController的过滤结果中选择多个表行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆