我有一個UISearchBar來搜索tableView中的一些數據,然後搜索後選擇一行打開一個detailViewController。爲此,有必要讓行保留索引路徑,並且不要在搜索後更改初始indexPath,否則我無法在數據庫中找到正確的元素。我如何保持最初的indexPath?還有其他方法嗎?使用UISearchBar後打開detailViewController
-(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
}
非常感謝你much.I未能創造** ** SearchResult中。我沒有獲得數據庫的元素數組,而是通過這種方式我可以提取元素。 –