我有下面的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];
}对于过滤表视图,它可以正常工作。
但如果:
然后
didSelectRowAtIndexPath被解雇了但是cellForRowAtIndexPath方法中,表达式
if(tableView==self.searchDisplayController.searchResultsTableView)返回假此外,如果UISearchView是活动的
有什么想法吗?
发布于 2014-01-15 11:16:28
做两个数组。
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.
}发布于 2014-07-10 15:53:21
要添加到前面的答案,还需要添加:
searchBar.delegate = self;然后,searchBar将能够调用
-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)texthttps://stackoverflow.com/questions/21135305
复制相似问题