我瘋了,試圖讓我的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;
}
我的確有shouldReloadTableForSearchString函數。那就是我調用函數來創建過濾數組並顯示並返回YES的地方。 – Dave 2014-09-25 18:21:25