/ Published in: Objective C
I needed to add an button to the right of the default section header in an UITableView, and I wanted to keep the default style of the headers.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; containerView.backgroundColor = [UIColor groupTableViewBackgroundColor]; CGRect labelFrame = CGRectMake(20, 2, 320, 30); if(section == 0) { labelFrame.origin.y = 13; } UILabel *label = [[UILabel alloc] initWithFrame:labelFrame]; label.backgroundColor = [UIColor clearColor]; label.font = [UIFont boldSystemFontOfSize:17]; label.shadowColor = [UIColor colorWithWhite:1.0 alpha:1]; label.shadowOffset = CGSizeMake(0, 1); label.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1.000]; label.text = [self tableView:tableView titleForHeaderInSection:section]; [containerView addSubview:label]; if(section == 1) { UIButton *abutton = [UIButton buttonWithType: UIButtonTypeContactAdd]; abutton.frame = CGRectMake(270,0 , 40, 40); [abutton addTarget: self action: @selector(addPage:) forControlEvents: UIControlEventTouchUpInside]; [containerView addSubview:abutton]; } return containerView; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if(section == 0) return 47; else { return 47-11; } } - (void) addPage:(id)sender { NSLog(@"addPage"); }