UITableViewController で奇妙な問題に直面しています。UISearchDisplayController に検索結果を表示します。検索結果にデータがある場合は問題ありませんが、結果が空の場合は、この SearchDC の背後にある UITableView のセクションが表示されます。検索と通常のテーブル ビューの両方に同じデリゲートとソースがあります。投稿に添付された画像を参照してください(1つは最初に、もう1つは最後にあります)。私は以下のような風邪をひいています:
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *pstrHeader = [[NSString alloc] init];
if(self.isSearchActivated)
{
NSInteger count = [self.parrSearchDataSourceKeys count];
if(count > 0)
pstrHeader = [pstrHeader stringByAppendingString:[self.parrSearchDataSourceKeys objectAtIndex:section]];
else
{
pstrHeader = nil;
[[[UIAlertView alloc]initWithTitle:@"header of section" message:@"Count is zero" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}
}
else
{
NSInteger count = [self.parrDataSourceKeys count];
if(count > 0)
pstrHeader = [pstrHeader stringByAppendingString:[self.parrDataSourceKeys objectAtIndex:section]];
else
pstrHeader = nil;
}
return pstrHeader;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
NSInteger count = 0;
if(self.isSearchActivated)
{
count = [self.parrSearchDataSourceKeys count];
if(count <= 0)
{
[[[UIAlertView alloc]initWithTitle:@"number of section" message:@"Count is zero" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}
}
else
count = [self.parrDataSourceKeys count];
return count;
}
デリゲートとソースに同じクラスがありますが、内部的には、通常のテーブル ビューと検索結果テーブル ビューで異なるマップと配列を処理します。
また、以下のような検索結果を表示します。
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSString *selectedTitle = [searchBar.scopeButtonTitles objectAtIndex: searchBar.selectedScopeButtonIndex];
selectedTitle = [self convertStringToEnglish:selectedTitle];
self.isSearchActivated = YES;
FolderInfo* searchFolder = [[FolderInfo alloc] init];
if(self.pmdSearchDataSource != nil)
{
[self.pmdSearchDataSource removeAllObjects];
}
if(self.parrSearchDataSourceKeys != nil)
{
[self.parrSearchDataSourceKeys removeAllObjects];
}
NSString* pstrSearchText = [searchBar text];
NSString* pstrSearchIn = @"Folder";
if([self.pstrWorkspaceId isEqualToString:@"-1"])
{
pstrSearchIn = @"Application";
}
else if([self.pstrFolderId isEqualToString:@"-1"])
{
pstrSearchIn = @"Project";
}
self.pmdSearchDataSource = [[NSMutableDictionary alloc] initWithDictionary:[searchFolder GetSearchListForSubElementFor:pstrSearchText InWorkspaceId:self.pstrWorkspaceId
InFolderId:self.pstrFolderId forSearchField:selectedTitle In:pstrSearchIn] copyItems:NO ];
NSArray *keys = [[self.pmdSearchDataSource allKeys] sortedArrayUsingSelector:@selector(compare:)];
self.parrSearchDataSourceKeys = [[NSMutableArray alloc] initWithArray:keys];
self.pSearchDC.searchResultsTableView.delegate = self;
self.pSearchDC.searchResultsTableView.dataSource = self;
[self.view bringSubviewToFront:self.pSearchDC.searchResultsTableView];
[self.pSearchDC.searchResultsTableView reloadData];
}
最初に通常のテーブル ビューに画面に 2 つのセクションが表示されている場合、searchResultDC に移動すると、最初に空白のテーブルが表示されますが、結果がないときに検索をクリックして searchResultDC tableview をリロードすると、この searchResultDC の背後にある通常のテーブル ビューのセクションが表示されます
私は投稿を参照しました: Reloading UITableView behind UISearchDisplayController
ただし、検索結果にもセクションが必要であることに注意してください。上記の投稿の解決策は、検索結果にセクションがない場合です。
さらに情報が必要な場合はお知らせください。