0

我有一個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 
} 

回答

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

} 
+0

非常感謝你much.I未能創造** ** SearchResult中。我沒有獲得數據庫的元素數組,而是通過這種方式我可以提取元素。 –

1

您可以通過信息搜索結果陣列生成搜索研究表視圖,在這種情況下,在信息搜索結果陣列的表視圖indexpath.row和對象的索引必須是相同的:因此使用以下

-(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); 

}

0

你應該ü se cell的標籤來代替。在的cellForRowAtIndexPath

cell.tag = indexPath.row; 

使用此代碼,然後indexpath回

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ 
     int ind = [tableView cellForRowAtIndexPath:indexPath].tag; 
     //now you have real indexpath 
    }