UITableView添加按钮部分

问题描述:

我是iOS编程新手。我正在构建一个课程管理系统的应用程序,该系统有一个导航的桌面视图,可以在不同课程之间进行导航我想添加按钮到表的每个部分,而不是有行。例如,如果课程CS101有5个项目,我想要5个图标(UIButton的)出现,当我点击名为CS101的标题排列成3行,2,2和1按钮代表五个项目。UITableView添加按钮部分

我该如何去做这件事?如果这可能?

非常感谢! Satyam

是的,这是可能的。你需要使用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

为此,你必须返回在此委托包含一个UIButton

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

上的动作一个UIView此按钮可以插入要显示为图标的行数,并且新插入的行将包含UIButton。插入行,你可以使用

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