gpt4 book ai didi

iphone - UISearchBar + didSelectRowAtIndexPath + cellForRowAtIndexPath

转载 作者:行者123 更新时间:2023-12-03 20:27:25 25 4
gpt4 key购买 nike

我有以下 UITableViewController + UISearchBar 设置

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

UISearchBar *searchBar;
UISearchDisplayController *searchDisplayController;
}

加载方法下方

- (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 出现时选择数据/行,我使用

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

它可以正常过滤表格 View 。

但是如果:

  • 搜索 View 处于事件状态(显示过滤结果)
  • 我点击过滤结果中的一行

然后

  • didSelectRowAtIndexPath 被触发,但是
  • cellForRowAtIndexPath方法中表达式

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

如果 UISearchView 处于事件状态

有什么想法吗?

最佳答案

创建两个数组。

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

关于iphone - UISearchBar + didSelectRowAtIndexPath + cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21135305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com