gpt4 book ai didi

ios - 使用 UISearchBar 后打开 detailViewController

转载 作者:行者123 更新时间:2023-11-28 22:41:59 25 4
gpt4 key购买 nike

我有一个 UISearchBar 用于在 tableView 中搜索一些数据,然后在搜索之后选择一行以打开 detailViewController 。为此,这些行必须保留索引路径并且在搜索后不要更改初始索引路径,否则我无法在数据库中找到正确的元素。如何保留初始的 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
}

最佳答案

这样做

- (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];
}

}

关于ios - 使用 UISearchBar 后打开 detailViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14274857/

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