ios UITableView 在节中添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5521057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView add buttons in section
提问by Satyam
i am new to iOS programming. I am building an application for a Course Management System which has a tableview for navigation to navigate between different courses. I want to add buttons to each section of table instead of having rows.
我是 iOS 编程的新手。我正在为课程管理系统构建一个应用程序,该系统具有用于导航的表格视图以在不同课程之间导航。我想向表格的每个部分添加按钮而不是行。
FOr example if course CS101 has 5 items under it, I want 5 icons (UIButton's) to appear when I click on the header named CS101 arranged in 3 rows with 2,2 and 1 button representing the five items.
例如,如果课程 CS101 下有 5 个项目,当我单击名为 CS101 的标题时,我希望出现 5 个图标(UIButton 的),该标题排成 3 行,其中 2,2 和 1 按钮代表这五个项目。
How do I go about doing this? If this possible?
我该怎么做?如果这可能吗?
Thanks much! Satyam
非常感谢!萨蒂扬
回答by Ajay Sharma
Yes it is possible.You need to use the delegate methods of UITableView and add View with all Buttons in it to TableView's header.
是的,这是可能的。您需要使用 UITableView 的委托方法并将其中包含所有按钮的 View 添加到 TableView 的标题中。
First set the Delegate & Datasource of UITableView then proceed with below code.
首先设置 UITableView 的 Delegate & Datasource 然后继续下面的代码。
For this ,You can follow the below code:
为此,您可以按照以下代码进行操作:
- (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;
}
Once a view is added to header then set the height for header since by default it would be something 20.,so you need to adjust it according to your View HEight that has been added to header.
将视图添加到标题后,然后设置标题的高度,因为默认情况下它将是 20.,因此您需要根据已添加到标题的视图高度来调整它。
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100.0;
}
Hope that is surely help you a lot... :)
希望这肯定对您有很大帮助... :)
回答by saadnib
for this you have to return a UIview in this delegate which contain a UIbutton
为此,您必须在此委托中返回一个包含 UIbutton 的 UIview
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
On the action of this button you can insert number of rows which you want to display as icons and new inserted rows will contain a UIButton. For inserting rows you can use
在此按钮的操作中,您可以插入要显示为图标的行数,新插入的行将包含一个 UIButton。对于插入行,您可以使用
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation