首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISearchBar + didSelectRowAtIndexPath + cellForRowAtIndexPath

UISearchBar + didSelectRowAtIndexPath + cellForRowAtIndexPath
EN

Stack Overflow用户
提问于 2014-01-15 10:45:51
回答 2查看 1.9K关注 0票数 2

我有下面的UITableViewController + UISearchBar设置

代码语言:javascript
复制
@interface FeedTableView : UITableViewController <UISearchBarDelegate,UISearchDisplayDelegate>
{
    NSMutableArray *ArrayDatiOriginali;
    NSMutableArray *searchData;

    UISearchBar *searchBar;
    UISearchDisplayController *searchDisplayController;    
}

负荷法以下

代码语言:javascript
复制
- (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.
}

若要在出现UISeach时选择数据/行,请使用

代码语言:javascript
复制
if(tableView==self.searchDisplayController.searchResultsTableView)
{
    NSLog(@"Riga corrente: %i",indexPath.row);
    appo=[searchData objectAtIndex:indexPath.row];
}

对于过滤表视图,它可以正常工作。

但如果:

  • 搜索视图是活动的(显示过滤的结果)
  • 我单击过滤结果的一行。

然后

  • didSelectRowAtIndexPath被解雇了但是
  • cellForRowAtIndexPath方法中,表达式 if(tableView==self.searchDisplayController.searchResultsTableView)返回假

此外,如果UISearchView是活动的

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2014-01-15 11:16:28

做两个数组。

dataArray & filteredDataArray &做一个BOOL isFiltered。

代码语言:javascript
复制
-(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.
}
票数 6
EN

Stack Overflow用户

发布于 2014-07-10 15:53:21

要添加到前面的答案,还需要添加:

代码语言:javascript
复制
searchBar.delegate = self;

然后,searchBar将能够调用

代码语言:javascript
复制
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21135305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档