2013-07-02 36 views
0

我有一個UISearchBar在超類中設置,我無法更改。我想創建一個UIView,將搜索欄添加到該視圖,然後在搜索欄上方添加一個按鈕和文本。我怎樣才能做到這一點。然後,我想將UIView設置爲表視圖的標題。以下是我用來設置searchBar實例變量的代碼。如何顯示UISearchBar上方的內容?

searchBar = (UISearchBar *)self.tableView.tableHeaderView; 

回答

1

這是如何編程創建一個標題視圖,並將其分配到表格視圖:

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)]; 
UISearchBar* mySearchBar = [[UISearchbar alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)]; 
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)]; 
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)]; 

[headerView addSubview:labelView]; 
[headerView addSubview:mySearchBar]; 
[headerView addSubview:myButton]; 
self.tableView.tableHeaderView = headerView; 

我會建議你從界面生成器做配置,它更容易。

+0

不起作用。沒有任何顯示 – mergesort

+0

嘗試設置標籤和按鈕的背景顏色,然後將它們添加到headerView:''labelView.backgroundColor = [UIColor redcolor];''和按鈕相同,以查看它們是否存在 – Teo