我有两个问题:
我正在尝试的代码:
- (void)viewDidLoad {
[super viewDidLoad];
const NSInteger searchBarHeight = 45;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0,
self.tableView.frame.size.width, searchBarHeight)];
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
[searchBar release];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Refresh"
style:UIBarButtonItemStyleBordered target:self action:@selector(onAddContact:)];
self.navigationItem.rightBarButtonItem = addButton;
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor]; // change this color
self.navigationItem.titleView = label;
label.text = NSLocalizedString(@"All Contacts", @"");
[label sizeToFit];
content = [DataGenerator wordsFromLetters];
indices = [[content valueForKey:@"headerTitle"] retain];
}
答案 0 :(得分:1)
您需要将搜索栏与表格视图分开。只需将searchBar添加到视图中,而不是将其添加到表格中。代码如下:
[self.view addSubview:searchBar];
然后添加表格视图。
对于UISearchDisplayController示例,请遵循以下教程: http://cocoabob.net/?p=67 要么 http://blog.mugunthkumar.com/coding/iphone-tutorial-uisearchdisplaycontroller-with-nspredicate/
也可以使用源代码。如果您不明白,请告诉我。我有一个项目,其中所有这些已经实施。
答案 1 :(得分:0)
此时当您滚动UITableView
时,UISearchBar
也会滚动{1}},因为您将UISearchBar
添加为UITableView
的标题视图,请参阅此行..
self.tableView.tableHeaderView = searchBar;
所以使用UITableView
进行滚动,这里用于修复UISearchBar
只是添加self.view
的子视图,但是在UITableView
以外的地方...
[yourSearchBar setFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:yourSearchBar];
有关详细信息,请参阅下面的UISearchBar
与UITableView
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
教程和演示的链接