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
是活跃的
有任何想法吗?
制作两个阵列.
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)
归档时间: |
|
查看次数: |
1786 次 |
最近记录: |