2012-11-09 138 views
1

我有一個帶有搜索欄的TableView。但我想添加另一個視圖(可以說是一個標籤)到TableView中(所以它與tableview一起滾動)。例如,如果您打開iPhone手機通訊錄應用程序,在所有號碼下,您可以看到一個名爲「我的號碼:」的標籤。它必須與搜索欄處於同一層級。我試圖在搜索欄上方添加一個新標籤,但故事板不允許我這樣做。使用搜索欄自定義UITableView

Document Outline Screenshot for the ViewController

在這裏,如果我拖放一個UILabel此層次結構,要麼替換搜索欄上方,或標籤原型細胞內下降。沒有其他方式一次顯示搜索欄和標籤。

有沒有辦法做到這一點?

回答

1

創建視圖控制器的視圖之外的單獨視圖(或裏面,也不要緊,你甚至可以以編程方式創建此),並將其鏈接到一個IBOutlet myCustomView。 創建IBOutlet搜索欄並鏈接您的UISearchBar

in viewDidLoad;你可以使用 [searchBar addSubView:myCustomView]; 並在搜索欄上方添加您的自定義視圖。

您可以顯示/隱藏(myCustomView.hidden = YES/NO)自定義視圖,或者在需要時將其從超級視圖中添加或刪除。

前。

- (IBAction)didTapShowCustomHeaderButton { 
    myCustomView.hidden = NO; 
} 

- (IBAction)didTapHideCustomHeaderButton { 
    myCustomView.hidden = YES; 
} 
0

添加下面的兩名代表在代碼

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    { 

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease]; 
     label.text = @"My Label"; 
    label.backgroundColor = [UIColor clearColor]; 

     return label; 

    } 

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{ 
     return 30 // this height should be equivalent to the myLabel height u declared in headerView above 
     } 
+0

嗨,感謝您的回覆。但是我需要這個標籤在滾動表格時使用搜索欄離開屏幕。 – sleepwalkerfx

+0

雅在哪裏想要它..我假設你需要它作爲tableview的一部分 – AppleDelegate

+0

我需要在運行時隱藏搜索欄,並顯示另一個UIcontrol當搜索欄被隱藏。爲了做到這一點,我認爲我必須將其他UIcontrol添加到搜索欄精確定位的位置(分層) – sleepwalkerfx