我有一個根視圖控制器,它組成了頂部的搜索欄和底部的子表視圖控制器。我使用的成分,而不是因爲這些原因指定的搜索欄,以表視圖的標題:UISearchDisplayController不完全覆蓋子視圖控制器
- 我不想索引與搜索欄(如聯繫人應用程序)重疊。
- 我希望搜索欄變得粘滯。也就是說,當我滾動表格視圖時它不會移動(再次像聯繫人應用程序)。
- 我的表格視圖已經有一個標題。
由於搜索欄位於根視圖控制器中,我還在根視圖控制器中實例化了我的搜索顯示控制器。我尋求建議的搜索UI有兩個問題:
- 半透明灰色疊加層不覆蓋整個子表視圖。它留下標題的頂部和索引可見。
- 同樣,搜索結果表不包括整個子表視圖。我知道如何手動更改此結果表格視圖的框架,但這樣做只能解決此問題......灰色半透明覆蓋圖的框架未鏈接到結果表視圖框架。他們沒有財產訪問覆蓋。
1)閒置
2)輸入搜索欄
3)開始輸入
#import "ContactsRootViewController.h"
#import "ContactsViewController.h"
#import "UIView+position.h"
#import "User.h"
#import "UserCellView.h"
#import "UserViewController.h"
@interface ContactsRootViewController()
@property(nonatomic, strong) UISearchBar* searchBar;
@property(nonatomic, strong) ContactsViewController* contactsViewController;
@property(nonatomic, strong) UISearchDisplayController* searchController;
@property(nonatomic, strong) NSMutableArray* matchedUsers;
@end
@implementation ContactsRootViewController
#pragma mark UIViewController
- (NSString*)title
{
return @"Contacts";
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.matchedUsers = [NSMutableArray array];
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.placeholder = @"Search";
[self.searchBar sizeToFit];
[self.view addSubview:self.searchBar];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (self.contactsViewController == nil) {
self.contactsViewController = [[ContactsViewController alloc] init];
[self addChildViewController:self.contactsViewController];
self.contactsViewController.view.frame = CGRectMake(
0.0,
self.searchBar.bottomY,
self.view.frame.size.width,
self.view.frame.size.height - self.searchBar.bottomY
);
self.contactsViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:self.contactsViewController.view];
[self.contactsViewController didMoveToParentViewController:self];
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self.contactsViewController];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
}
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.matchedUsers.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* identifier = @"contactsRootViewUserCell";
UserCellView* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UserCellView alloc] initWithIdentifier:identifier];
}
cell.user = [self.matchedUsers objectAtIndex:indexPath.row];
return cell;
}
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController pushViewController:[[UserViewController alloc] initWithUser:[self.matchedUsers objectAtIndex:indexPath.row]] animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [UserCellView height];
}
#pragma mark UISearchDisplayControllerDelegate
- (BOOL)searchDisplayController:(UISearchDisplayController*)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self.matchedUsers removeAllObjects];
searchString = [searchString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (searchString.length > 0) {
for (User* user in self.contactsViewController.allUsers) {
NSRange match = [user.userDisplayName rangeOfString:searchString options:NSCaseInsensitiveSearch];
if (match.location != NSNotFound) {
[self.matchedUsers addObject:user];
}
}
}
return YES;
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
[self.searchBar resignFirstResponder];
}
@end