2014-09-25 26 views
1

我瘋了,試圖讓我的IOS搜索欄在Iphone上工作。我從遠程服務器訪問數據並填充內容文件。然後我做一個過濾器來創建一個過濾的內容文件。然後我做一個[self.tableView reloadData()]。它第一次正常工作。然後,我改變我的範圍,並從我的服務器再次獲取數據並過濾它,並執行另一次重新加載。但是,表格第二次顯示前一個顯示的前9個項目,而不是來自過濾文件的新9個項目。我控制檯在過濾的文件中顯示文件計數,在這種情況下,該文件在tableView numberOfRowsInSection中爲9:我還顯示通過cellForRowAtIndexPath的每個項目。在cellForRowAtIndexPath中,我顯示了正確的9個未經過濾的項目,但它們不會顯示在桌面上!該表格顯示舊顯示的前9個項目。IOS搜索欄返回錯誤的數據

我的問題是,即使計數是正確的,新的數據顯示在表格上而不是舊的數據?爲什麼我在控制檯上顯示正確的項目,但顯示屏卻顯示舊顯示中的項目。我需要做些什麼來使新數據出現?我知道這很難理解,但我在下面列出了一些我的代碼,希望有人能夠告訴我爲什麼表視圖沒有被最新數據更新。

 // This is where I get data back from the server. 

    self.listContent = [[NSArray alloc] init]; 

    if(_scopeIndex == 0) 
     self.listContent = [jsonObject objectForKey:@"burials"]; 
    else 
     if(_scopeIndex == 1) 
      self.listContent = [jsonObject objectForKey:@"obits"]; 
     else 
      self.listContent = [jsonObject objectForKey:@"photos"]; 

    if(self.listContent > 0) 
    { 
    dispatch_async(dispatch_get_main_queue(), ^{ 

      [self filterContentForSearchText:searchString scope: 
      [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]]; 
     [self.tableView reloadData]; 
    }); 
    } 

以下是數據過濾的地方。在這種情況下,未過濾和過濾的文件是相同的。

 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{ 
/* 
Update the filtered array based on the search text and scope. 
*/ 

[self.filteredListContent removeAllObjects];// First clear the filtered array. 

/* 
Search the listContent for names that match and add to the filtered array. 
*/ 

for (int i = 0; i < [self.listContent count]; i++) 
{ 

     NSComparisonResult result = [self.listContent[i][@"LastName"] compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])]; 
     if (result == NSOrderedSame) 
     { 
      [self.filteredListContent addObject:self.listContent[i]]; 
     } 
// } 
} 

}

這是我得到的過濾項的表數。

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
if (tableView == self.searchDisplayController.searchResultsTableView) 
{ 
    NSLog(@"Filtered Name count = %i", [self.listContent count]); 
    return [self.filteredListContent count]; 
} 
else 
{ 
    NSLog(@"Name count = %i", [self.listContent count]); 
    return [self.listContent count]; 
} 

}

,這是我更新細胞在我的表:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *kCellID = @"cellID"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID]; 
if (cell == nil) 
{ 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
} 

if(indexPath.row > [self.filteredListContent count] - 1) 
    return cell; 

NSDictionary *burial = [self.filteredListContent objectAtIndex:indexPath.row]; 
NSString *lastname = burial[@"LastName"]; 
NSString *firstname = burial[@"FirstName"]; 

NSString *burialname = [NSString stringWithFormat: @"%@, %@", lastname, firstname]; 
cell.textLabel.text = burialname; 


NSLog(@"Cell name= %@ index path=%i", cell.textLabel.text, indexPath.row); 
return cell; 

}

回答

0

你如何使用搜索欄?邏輯應該是你有所有數據的tableview,你使用搜索欄過濾出匹配的結果,並將它們添加到搜索數據數組中,並且當搜索欄處於活動狀態時,顯示搜索數據數組。

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 

地方使用呢?

添加以下

如果您在本功能設置您可以更新搜索陣列,並使用類似

if (self.searchDisplayController.searchResultsTableView != nil) 
{ 
    //Updates searchData array 
    [self searchDisplayController:self.searchDisplayController shouldReloadTableForSearchString:searchBar1.text]; 

    //Updates display 
    [self.searchDisplayController.searchResultsTableView reloadData]; 

} 
else 
{ 
    [_channelsTableView reloadData]; 
} 

它調用searchDisplayController與當前的搜索欄文本重新加載它。當新的tableview數據來自服務器時,您可以激活searchDisplayController來刷新搜索結果數組,然後可以刷新顯示。

+0

我的確有shouldReloadTableForSearchString函數。那就是我調用函數來創建過濾數組並顯示並返回YES的地方。 – Dave 2014-09-25 18:21:25

0

有些事情嘗試:

  1. 也許你太早重裝的tableView,或
  2. cellForRowAtIndexPath需求表視圖之間區分(就像你numberOfRowsInSection一樣),或
  3. 也許你沒有正確設置所有代表。 searchBar與UISearchDisplayController一起使用,其中有2位代表:searchResultsDataSourcesearchResultsDelegate。確保這些設置爲self(或任何類處理這些)。
+0

我有以下代表集:[searchDisplayController setDelegate:self]; \t [searchDisplayController setSearchResultsDataSource:self]; [searchDisplayController searchResultsDelegate:self];但是,當我嘗試添加[searchDisplayController searchResultsDelegate:self];我得到'UISearchDisplay COntroller'的錯誤'No visible @interface'聲明瞭選擇器'search ResultsDelegate'。 – Dave 2014-09-25 18:23:03

+0

@Dave有兩種風格的語法可以使用'[searchDisplayController setSearchResultsDelegate:self]'或'searchDisplayController.searchResultsDelegate = self;'我想你在語句中省略了'set'。 – Rayfleck 2014-09-25 21:50:10

+0

我改變它使用setSearchResultsDelegate:self,但它沒有在結果上有任何區別。我在控制檯顯示cellForRowAtIndexPath函數中的正確條目,但它們不顯示在視圖上。它顯示正確的條目數量,但列出了前9箇舊條目。這真是令人困惑。 – Dave 2014-09-26 01:13:46

1

我改變了我的邏輯去服務器一次獲取我的內容,這次我的內容表中包含範圍指標。這使我能夠處理範圍過濾器,而無需返回服務器查找特定範圍的數據。這樣做會導致在更改範圍時顯示正確的查看錶。我不會建議在搜索範圍發生變化時以非常週期的方式進入服務器,因爲它確實會將查看錶搞亂。