UISearchBar + didSelectRowAtIndexPath + cellForRowAtIndexPath

use*_*643 2 iphone objective-c uitableview uisearchbar uisearchdisplaycontroller

我有以下UITableViewController+ UISearchBar设置

@interface FeedTableView : UITableViewController <UISearchBarDelegate,UISearchDisplayDelegate>
{
    NSMutableArray *ArrayDatiOriginali;
    NSMutableArray *searchData;

    UISearchBar *searchBar;
    UISearchDisplayController *searchDisplayController;    
}
Run Code Online (Sandbox Code Playgroud)

在加载方法下面

- (void)viewDidLoad
{
    [super viewDidLoad];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate=self;

    self.tableView.tableHeaderView = searchBar;  tableView.
}
Run Code Online (Sandbox Code Playgroud)

当我使用UISeach时选择数据/行

if(tableView==self.searchDisplayController.searchResultsTableView)
{
    NSLog(@"Riga corrente: %i",indexPath.row);
    appo=[searchData objectAtIndex:indexPath.row];
}
Run Code Online (Sandbox Code Playgroud)

它适用于过滤表视图.

但如果:

  • 搜索视图处于活动状态(显示过滤结果)
  • 我点击过滤结果的一行

然后

  • didSelectRowAtIndexPath 被解雇但是
  • cellForRowAtIndexPath方法中表达

    if(tableView == self.searchDisplayController.searchResultsTableView)返回FALSE

如果UISearchView是活跃的

有任何想法吗?

Xei*_*han 6

制作两个阵列.

dataArray&& filteredDataArray 制作一个BOOL isFiltered.

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0)
{
    isFiltered = FALSE;
}
else
{
    isFiltered = true;
}
[self.tableView reloadData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
isFiltered = FALSE;
[searchBar resignFirstResponder];
[self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(isFiltered)
{
    <#YOUR_MODEL#>= [filteredDataArray objectAtIndex:indexPath.row];
}
else
{
    <#YOUR_MODEL#>= [dataArray objectAtIndex:indexPath.row];
}
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rowCount;
if(isFiltered)
    rowCount = [filteredDataArray count];
else
    rowCount = [dataArray count];
return rowCount;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// [tableView deselectRowAtIndexPath:indexPath animated:YES];
if(isFiltered)
{
    <#YOUR_MODEL#>= [filteredDataArray objectAtIndex:indexPath.row];
}
else
{
    <#YOUR_MODEL#>= [dataArray objectAtIndex:indexPath.row];
}
//Pass it to any class or do what ever.
}
Run Code Online (Sandbox Code Playgroud)