0

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

3 回答 3

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

}
于 2013-01-11T09:25:19.343 回答
1

您可能在表视图数据方法中传递 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);

}

于 2013-01-11T09:26:27.637 回答
0

您应该改用单元格的标签进行识别。在 cellForRowAtIndexPath 中使用此代码

 cell.tag = indexPath.row;

然后取回 indexpath

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
        int ind = [tableView cellForRowAtIndexPath:indexPath].tag;
        //now you have real indexpath
    }
于 2013-01-11T09:30:41.343 回答