使用 UISearchBar 后打开一个 detailViewController
Posted
技术标签:
【中文标题】使用 UISearchBar 后打开一个 detailViewController【英文标题】:Open a detailViewController after use of UISearchBar 【发布时间】:2013-01-11 09:11:38 【问题描述】:我有一个 UISearchBar 来搜索 tableView 中的一些数据,然后在搜索后选择一行来打开 detailViewController 。为此,行必须保持索引路径,并且在搜索后不要更改初始 indexPath,否则我无法在数据库中找到正确的元素。如何保留初始 indexPath?还有其他方法吗?
-(void) searchExpositorsTableView
[searchResult removeAllObjects];
//In this array there are the elements resulted after research
for (Database *currow in self.mutableArray)
NSLog(@"list of expositors %@", self.mutableArray);
NSRange titleResultsRange = [currow.name rangeOfString:self.searchBar.text options: NSCaseInsensitiveSearch];;
if (titleResultsRange.length >0)
[searchResult addObject:currow.name];
/*While I build this new array every indexPath change and the database
can not track down the original items*/
NSLog(@"%@", searchResult);
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
if(canSelectRow)
NSLog(@"%d", indexPath.row);
/*the indexPath is new and the database can not track down the original items*/
return indexPath;
else
return nil;
NSLog(@"%d", indexPath.row);
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//Open wrong element
【问题讨论】:
【参考方案1】:这样做
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(searching) // where searching is boolean YES is searching, NO if not searched any element
//get your data from searchResults and open detailViewController
Database *currow = [searchResults objectAtIndex:indexPath.row];
DetailViewController = detailView = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
else
//get your data from self.mutableArray and open detailViewController
Database *currow = [self.mutableArray objectAtIndex:indexPath.row];
DetailViewController = detailView = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
【讨论】:
非常感谢。我未能创建 SearchResult。我确实得到了数据库的元素数组,而不是字符串数组,这样我就可以提取元素了。【参考方案2】:您可能在表视图数据方法中传递 searchResult 数组以生成搜索研究表视图,在这种情况下,您的表视图 indexpath.row 和 searchResult 数组中的对象索引必须相同:所以使用以下
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
if(canSelectRow)
NSString * name = [searchResult objectAtIndex:indexPath.row] ;
/*
or you should instead save the Database object in searchResult Array directly instead of currow.name , in that case
Database *currow = [searchResult objectAtIndex:indexPath.row] ;
*/
else
return nil;
NSLog(@"%d", indexPath.row);
【讨论】:
【参考方案3】:您应该使用单元格的标签来代替。在 cellForRowAtIndexPath 中使用此代码
cell.tag = indexPath.row;
然后取回索引路径
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
int ind = [tableView cellForRowAtIndexPath:indexPath].tag;
//now you have real indexpath
【讨论】:
以上是关于使用 UISearchBar 后打开一个 detailViewController的主要内容,如果未能解决你的问题,请参考以下文章
UISearchBar在Swift中完成后如何停止UIActivityIndicator
通过 UISearchBar 过滤后 UITableView 不滚动顶部