ios - UITableView 在部分添加按钮

标签 ios uitableview uibutton

我是 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/

相关文章:

ios - 从 UITableView 中删除值会导致崩溃

ios - 什么时候在 NSManagedObject 上调用 initWithCoder?

ios - 更改 TableView 单元格颜色将不起作用

ios - 同一行中 UIButton 的自动布局约束

iphone - 如何随机放置 UIButton 和值

ios - 如果禁用了通知并且应用程序在应用程序商店中处于事件状态,我可以将推送通知弹出窗口发送到我的iPhone应用程序吗?

android - 不使用应用程序 Radbeacon 检测信标?

ios - 单击表格 View 单元格上的按钮时如何获取 indexPath.row?

iphone - 在基于导航的应用程序中加载 UITableView 时出现问题

iphone - 使用 UIButton 按下传递对象数据