2

後編輯:prepareForSegue UISearchDisplayController

我才發現:How to segue from a UISearchBarDisplayController result to a detailViewController

我將看看!


我將'搜索欄和搜索顯示控制器'與核心數據fetchedResultsController結合使用故事板。也就是說,我要區分之間:

if (self.tableView == self.searchDisplayController.searchResultsTableView) { 
    ... 

,並在那裏我剛剛上市從數據存儲中提取結果的情況下。

但是,我無法得到正確的行(索引路徑),在下面的情況:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"StoreDetails"]) { 
     UINavigationController *navigationController = segue.destinationViewController; 
     StoreDetailsTableViewController *controller = (StoreDetailsTableViewController *)navigationController.topViewController; 
     controller.managedObjectContext = self.managedObjectContext; 

     Store *storeToDetail = nil; 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; // This gives wrong row when having searched using searchDisplayController 
     NSLog(@"Row: %i", indexPath.row);             // row is always 0 
     if (self.tableView == self.searchDisplayController.searchResultsTableView) { 
      storeToDetail = [self.filteredListContent objectAtIndex:indexPath.row]; 
     } else { 
      storeToDetail = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
     } 

     controller.storeToDetail = storeToDetail; 
    } 
} 

這被稱爲後:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller  shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString scope:@"All"]; 
    ... 

有:

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    [self.filteredListContent removeAllObjects]; 

    for (Store *store in [self.fetchedResultsController fetchedObjects]) 
    { 
     if ([scope isEqualToString:@"All"] || [store.name isEqualToString:scope]) 
     { 
      NSComparisonResult result = [store.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])]; 
      if (result == NSOrderedSame) 
      { 
       [self.filteredListContent addObject:store]; 
      } 
     } 
    } 
} 

這取自蘋果的TableSearch示例。

原始問題是雙重的:

1)self.tableView似乎不等於self.searchDisplayController.searchResultsTableView被prepareForSegue

2),其具有搜索的indexPath(行)總是0

我想我可以使用didSelectRow ...來代替或組合使用,但我相信準備...應該有可能?!另外,在嘗試didSelectRow時...我確定如何將對象從相關行傳遞到目標控制器。也就是說,我能得到正確的indexPath在didSelectRow ...但我只知道如何在preparFor的SEGUE目的地...:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Store *storeToDetail = nil; 
    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     storeToDetail = [self.filteredListContent objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     storeToDetail = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    } 

    // How to get segue destination controller and set object?! 
    // Before doing: 
    [self performSegueWithIdentifier:@"StoreDetails" sender:self]; 
} 

任何幫助深表感謝。也許是一個教程的參考,它展示瞭如何組合這些東西。

謝謝!

回答

1

- (無效)prepareForSegue:(UIStoryboardSegue *)賽格瑞發件人:(ID)發送方

{

如果(self.filteredListContent)

{//的NSLog(@「後濾波的數據「;}

其他

{//的NSLog(@」 後的所有「;}

}

4

在這種情況下sender參數prepareForSegue:sender:被設置爲啓動segue的表格視圖單元格。

只需添加一個屬性,你的細胞保持Store屬於該單元格,你不必做任何索引路徑或表視圖比較舞蹈。

您應該結束了與作爲

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    FooCell *cell = (FooCell *)[self.tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER]; 
    Foo *foo = [self fooForIndexPath:indexPath]; 

    cell.foo = foo; 
    // additional initialization 

    return cell; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"view detail"]) { 
     DetailViewController *destViewController = segue.destinationViewController; 
     destViewController.foo = [(FooCell *)sender foo]; 
    } 
} 
+0

哦。我的。天哪。你剛剛改變了我的生活。 我只是增加了一個數據ID參數來我的UITableViewCell子類(其已經進一步在我的項目子類爲每一個細胞),而現在,如你所說,我並不需要擔心那些惱人的索引路徑或表視圖比較了! 我真的希望我能不止一次地投票。再次感謝! – 2013-11-23 10:13:50

0

您使用了錯誤的測試一樣簡單。

if (self.tableView == self.searchDisplayController.searchResultsTableView) { 
    ... 

這總是會失敗,你問是否兩個不同的tableView是不同的。答案總是否定的。

但是,您可以測試發件人是否來自searchResultsTableView。

NSIndexPath *searchIndexPathOfSelectedCell = [self.searchDisplayController.searchResultsTableView indexPathForCell:sender]; 

if (searchIndexPathOfSelectedCell) { 
    ...