2011-04-02 89 views
5

我是iOS編程新手。我正在構建一個課程管理系統的應用程序,該系統有一個導航的桌面視圖,可以在不同課程之間進行導航我想添加按鈕到表的每個部分,而不是有行。例如,如果課程CS101有5個項目,我想要5個圖標(UIButton的)出現,當我點擊名爲CS101的標題排列成3行,2,2和1按鈕代表五個項目。UITableView添加按鈕部分

我該如何去做這件事?如果這可能?

非常感謝! Satyam

回答

21

是的,這是可能的。你需要使用UITableView的委託方法,並添加View中的所有按鈕到TableView的頭部。

首先設置委託人& UITableView的數據源然後繼續下面的代碼。

對於這一點,你可以按照下面的代碼:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
// create the parent view that will hold header Label 
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]; 

// create the button object 
UIButton * headerBtn = [[UIButton alloc] initWithFrame:CGRectZero]; 
headerBtn.backgroundColor = [UIColor clearColor]; 
headerBtn.opaque = NO; 
headerBtn.frame = CGRectMake(10.0, 0.0, 100.0, 30.0); 
[headerBtn setTitle:@"<Put here whatever you want to display>" forState:UIControlEventTouchUpInside]; 
[headerBtn addTarget:self action:@selector(ActionEventForButton:) forControlEvents:UIControlEventTouchUpInside]; 
[customView addSubview:headerBtn]; 

return customView; 
} 

視圖一旦被添加到頭部,然後默認設置爲標題的高度,因爲它會是什麼20,所以你需要調整它根據已添加到標題的View HEight。

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
return 100.0; 
} 

希望這會幫助你很多... :)

+0

謝謝你幫助!我使用UITableViewCell創建了自己的表格單元格,並使用界面構建器使用兩個按鈕創建了自己的表格單元格,然後使用了UITableViewDelegate和DatasourceDelegate的一系列委託。 – Satyam 2011-04-06 07:46:03

+0

是的。 ...謝謝 :) – 2011-04-06 12:41:46

1

爲此,你必須返回在此委託包含一個UIButton

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

上的動作一個UIView此按鈕可以插入要顯示爲圖標的行數,並且新插入的行將包含UIButton。插入行,你可以使用

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation