ios - UISearchBar 在搜索结果中显示多个部分标题
<p><p>我有一个应用程序,当有人使用搜索栏过滤 UITableView 中的对象时,会显示两个部分标题。一个是静止的,另一个随着搜索结果移动。请参阅附上的两个屏幕截图。</p>
<p>下面我已经包含了我的应用程序中的 tableview 方法,任何帮助或建议将不胜感激。</p>
<p> <img src="/image/PCKyJ.png" alt="enter image description here"/> </p>
<p> <img src="/image/g3GvS.png" alt="enter image description here"/> </p>
<pre><code> - (void) performFetch
{
;
// Init a fetch request
NSFetchRequest *fetchRequest = [ init];
NSEntityDescription *entity = ;
;
// Apply an ascending sort for the color items
//NSSortDescriptor *sortDescriptor = [ initWithKey:@"Term" ascending:YES selector:nil];
NSSortDescriptor *sortDescriptor;
if(sortValue==1)
{
sortDescriptor = [ initWithKey:@"fullName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
}
else if(sortValue==2)
{
sortDescriptor = [ initWithKey:@"firstName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
}
else if(sortValue==3)
{
sortDescriptor = [ initWithKey:@"socialSecurity" ascending:YES selector:@selector(caseInsensitiveCompare:)];
}
NSArray *descriptors = ;
;
// Recover query
NSString *query = self.searchDisplayController.searchBar.text;
if (query && query.length) fetchRequest.predicate = %@) || (socialSecurity contains %@)",query, query];
// Init the fetched results controller
NSError *error;
if(sortValue==1)
{
self.fetchedResultsController = [ initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"pLLetter" cacheName:nil];
}
else if(sortValue==2)
{
self.fetchedResultsController = [ initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"pFLetter" cacheName:nil];
}
else if(sortValue==3)
{
self.fetchedResultsController = [ initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"pSLetter" cacheName:nil];
}
self.fetchedResultsController.delegate = self;
if (![ performFetch:&error]) NSLog(@"Error: %@", );
;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
;
;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
;
}
#pragma mark - Table View
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [ count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [ objectAtIndex:section];
return ;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [ objectAtIndex:section];
return ];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
if (index == 0) {
// search item
bounds] animated:NO];
return -1;
}
return index-1;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
// Return the array of section index titles
NSArray *searchArray = ;
return ;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = ;
if (cell == nil) {
if(subtitleValue==1){cell = [ initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];}
else {cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
;
return cell;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我遇到了同样的问题,这解决了它:在“performFetch()”中搜索结束时,不要调用 tableView.reloadData(),而是调用 </p>
<pre><code> ;
</code></pre>
<p>原因:搜索后,有一个新的tableview,看这里:<a href="https://stackoverflow.com/a/6971837/1697613" rel="noreferrer noopener nofollow">UITableView Plain Style Section Header gets Redrawn on Search View</a> .</p></p>
<p style="font-size: 20px;">关于ios - UISearchBar 在搜索结果中显示多个部分标题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25255954/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25255954/
</a>
</p>
页:
[1]