Sat*_*yam 5 uibutton uitableview ios
我是iOS编程的新手.我正在构建一个课程管理系统的应用程序,该系统具有导航的桌面视图,可以在不同的课程之间导航.我想为表的每个部分添加按钮而不是有行.
例如,当课程CS101下面有5个项目时,我想要5个图标(UIButton)出现,当我点击名为CS101的标题排列成3行时,2,2和1个按钮代表五个项目.
我该怎么做呢?如果这可能吗?
非常感谢!萨蒂扬
Aja*_*rma 21
是的,这是可能的.您需要使用UITableView的委托方法,并将View中的所有按钮添加到TableView的标题中.
首先设置UITableView的Delegate和Datasource然后继续下面的代码.
为此,您可以按照以下代码:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
一旦将视图添加到标题然后设置标题的高度,因为默认情况下它将是20,因此您需要根据已添加到标题的View HEight进行调整.
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100.0;
}
Run Code Online (Sandbox Code Playgroud)
希望这肯定能帮到你很多...... :)
| 归档时间: |
|
| 查看次数: |
20223 次 |
| 最近记录: |