开发者

UITableView add buttons in section

开发者 https://www.devze.com 2023-02-22 02:02 出处:网络
i am new to iOS programming. I am building an application for a Course Man开发者_运维百科agement System which has a tableview for navigation to navigate between different courses. I want to add button

i am new to iOS programming. I am building an application for a Course Man开发者_运维百科agement 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


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... :)


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
0

精彩评论

暂无评论...
验证码 换一张
取 消