在TableView上更改UISearchBars的宽度

时间:2011-06-06 14:49:31

标签: iphone objective-c xcode ios tableview

我需要在tableView中创建两个UISearchBars。我希望它们两个宽度相等(在桌子的顶部)。

我已经为UISearchBar创建了两个出口,并为它们创建了属性和de alloc。我发现很难在视图中放置它们(我的意思是合适)。我只有一个搜索栏扩展到屏幕的全宽。我怎么适合两个?

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 4, 45)];
zipSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(4, 0, 4, 45)];

searchBar.placeholder = @"School Name";
zipSearchBar.placeholder=@"Zip";

searchBar.delegate = self;
zipSearchBar.delegate = self;

self.tableView.tableHeaderView= zipSearchBar;
self.tableView.tableHeaderView= searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;

1 个答案:

答案 0 :(得分:3)

当您执行self.tableView.tableHeaderView= searchBar;时,您只需重新分配它。您必须构建一个包含搜索栏的视图,然后将其设置为表的标题视图。像这样的东西,

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
zipSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(200, 0, 120, 45)];

searchBar.placeholder = @"School Name";
zipSearchBar.placeholder=@"Zip";

searchBar.delegate = self;
zipSearchBar.delegate = self;

UIView * comboView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
[comboView addSubview:searchBar];
[comboView addSubview:zipSearchBar];

self.tableView.tableHeaderView= comboView;
[comboView release];