我遇到了 UITableViewController 的奇怪问题。我在 UISearchDisplayController 中显示搜索结果。如果我在搜索结果中有一些数据,那很好,但是当我有空结果时,它将显示位于此 SearchDC 后面的 UITableView 部分。对于搜索和普通表视图,我有相同的委托和来源。参考附有帖子的图片(一个在开头,一个在结尾)。我有如下所示的colde:
- (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];
}
如果最初正常的表格视图在屏幕上可见两个部分,然后我移至 searchResultDC,它将首先显示空白表格,但在我没有结果并重新加载 searchResultDC 表格视图时单击搜索后,它将显示此 searchResultDC 后面的普通表格视图部分
我参考了帖子: 在 UISearchDisplayController 后面重新加载 UITableView
但请注意,我也需要搜索结果中的部分,上面帖子中的解决方案是针对我们在搜索结果中没有部分的情况。
如果您需要更多信息,请与我们联系。