我是 iOS 编程新手。我正在为类(class)管理系统构建一个应用程序,该系统具有用于导航的表格 View 以在不同类(class)之间导航。我想向表格的每个部分添加按钮而不是有行。
例如,如果类(class) CS101 下有 5 个项目,我希望在单击名为 CS101 的标题时出现 5 个图标(UIButton 的),该标题排列成 3 行,其中 2,2 和 1 个按钮代表这五个项目。
我该怎么做呢?如果可能的话?
非常感谢! 萨蒂扬
最佳答案
是的,这是可能的。您需要使用 UITableView 的委托(delegate)方法,并将其中包含所有按钮的 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;
}
将 View 添加到标题后,设置标题的高度,因为默认情况下它是 20 左右,因此您需要根据添加到标题的 View 高度调整它。
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100.0;
}
希望这对您有很大帮助...:)
关于ios - UITableView 在部分添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5521057/