2013-08-27 34 views
0

我有一個簡單的問題,我正在尋找最有效的方式來處理這個問題。刪除searchResultsTableView和mainTableView之間的同步

我有一個主表(稱爲mainTableView),它有searchResultsTableView控制的搜索欄。

我的主表有一個可變數組,說mainItems的說10個項目。

當執行搜索時,searchResultsTableView可以包含發言權不同可變數組3項說searchedItems

現在在搜索控制器我刪除2出3項,我也從searchedItems和searchResultsTableView刪除。這些是一次刪除1。

因此,當我從searchItems中刪除時,我還需要從mainItems中刪除以保持同步,但索引會在mainItems中每次刪除時都保持更改,所以如何知道要在mainItems中刪除的原始索引?

我應該尋找一些基於字典的方法而不是數組嗎?

回答

0

不能比較數組內的對象並刪除使用索引。

這樣說,如果從searchedItems刪除的產品deletedObj

int indexToDelete = -1; 
for(i=0; i<[mainItems length]; i++){ 
    Object * myMainObj = mainItems[i]; 
    if (myMainObj isEqual:deletedObj) //this will work depends on the object inside your datasource 
    { 
     indexToDelete = i; 
     break; 
    } 
} 
if(indexToDelete != -1){ 
    [mainItems removeObjectAtIndex:indexToDelete]; 
} 
+0

這將工作,但我相信這是做的最inifficient方式之一。 – theiOSguy

+0

然後我想,你可以按照你的建議使用詞典,只需從searchItems和mainItems中刪除相同的鍵。應該管用! – HRM