Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView add buttons in section

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.

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.

How do I go about doing this? If this possible?

Thanks much! Satyam

like image 548
Satyam Avatar asked Apr 02 '11 04:04

Satyam


2 Answers

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.

First set the Delegate & Datasource of UITableView then proceed with below code.

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.

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
 {
 return 100.0;
 }

Hope that is surely help you a lot... :)

like image 59
Ajay Sharma Avatar answered Nov 02 '22 21:11

Ajay Sharma


for this you have to return a UIview in this delegate which contain a UIbutton

- (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

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
like image 1
saadnib Avatar answered Nov 02 '22 22:11

saadnib