2011-06-06 28 views
0

我需要在我的tableView中創建兩個UISearchBars。我希望他們在表格頂部的寬度相同(並排)。更改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"; 
[email protected]"Zip"; 

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

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

回答

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"; 
[email protected]"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]; 
+0

FML!我拿着所有在線文章說我不能改變UISearchBar的寬度。非常感謝@DEEPAK。 – Legolas 2011-06-06 16:09:43

+0

@Deepak:http://stackoverflow.com/questions/6258001/real-time-search-in-uitableview – Legolas 2011-06-06 21:06:34

+0

看到它。我不認爲有。 – 2011-06-06 21:07:03