2014-04-25 46 views
1

我有一個search bartableview。在搜索時,我會在搜索結果中顯示tableView。現在我從tableView中選擇一行並導航到detail view,其中我將textField顯示爲最近選擇的tableCell的文本。我更新了文本框。並代表回到之前的tableViewController。這裏我想更新過濾表格視圖中的單元格以及數據源中的主數據數組。如何在iOS中執行此操作。如何在搜索操作後更新過濾tableView中的tableViewCell數據?

+0

豈不是一樣的非搜索結果即'[UITableView的reloadRowsAtIndexPaths: withRowAnimation:'',只需使用不同的索引路徑? – trojanfoe

+0

感謝trojanfoe,這對我來說是真正的挑戰。如何跟蹤未搜索和搜索結果中的索引? – Ganesh

回答

0

該方法將有助於在任何階段對錶格數據進行重新排序。​​

1

如果您使用的是UISearchViewController,則需要訪問正確的表視圖以進行重新加載操作。一個可能的解決方案(如果你有一個屬性searchViewController):

[self.searchViewController.searchResultsTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationFade]; 
+0

非常感謝。但如何跟蹤已過濾數組的原始數據數組?我需要在兩個陣列中更新它。如何映射兩個數組的索引路徑。這對我來說是真正的挑戰。 – Ganesh

1

試試這個,

DetailViewControllerNSIndexPath *selectedIndexPath .H添加@property

@property (nonatomic, strong) NSIndexPath *selectedIndexPath; 

設置的selectedIndexPath屬性DetailViewController in didSelectRow方法

delegate方法在tableViewController添加NSIndexPath參數,如

- (void)didUpdatedText:(NSString *)updatedText atIndexPath:(NSIndexPath *)indexPath; 

編輯:

- (void)didUpdatedText:(NSString *)updatedText atIndexPath:(NSIndexPath *)indexPath { 

    if (isSearching) { 
     NSString *updatingText = [self.filteredArray elementAtIndex:indexPath.row]; 
     int elementIndex = [self.dataSourceArray indexOfElement:updatingText]; 
     [self.dataSourceArray replaceObjectAtIndex:elementIndex withObject:updatedText]; 
     [self.filteredArray replaceObjectAtIndex:indexPath.row withObject:updatedText]; 
    } else { 
     [self.dataSourceArray replaceObjectAtIndex:indexPath.row withObject:updatedText]; 
    } 
    [self.tableView beginUpdates]; 
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade]; 
    [self.tableView endUpdates]; 
} 
+0

非常感謝。但如何跟蹤已過濾數組的原始數據數組?我需要在兩個陣列中更新它。如何映射兩個數組的索引路徑。這對我來說是真正的挑戰。 – Ganesh

+0

由於您是從原始數據數組進行過濾,因此過濾數組中的元素具有相同的引用。因此,更新過濾數組中的元素依次更新原始數據數組中的元素(如前所述,兩者具有相同的參考)。但是在這裏元素本身發生了變化,所以嘗試了不同的方式。我更新了代碼。 – Akhilrajtr

相關問題