Recreate the default section headers in Rounded UITableview


/ Published in: Objective C
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  2. UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
  3. containerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
  4. CGRect labelFrame = CGRectMake(20, 2, 320, 30);
  5. if(section == 0) {
  6. labelFrame.origin.y = 13;
  7. }
  8. UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
  9. label.backgroundColor = [UIColor clearColor];
  10. label.font = [UIFont boldSystemFontOfSize:17];
  11. label.shadowColor = [UIColor colorWithWhite:1.0 alpha:1];
  12. label.shadowOffset = CGSizeMake(0, 1);
  13. label.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1.000];
  14. label.text = [self tableView:tableView titleForHeaderInSection:section];
  15. [containerView addSubview:label];
  16. if(section == 1) {
  17. UIButton *abutton = [UIButton buttonWithType: UIButtonTypeContactAdd];
  18. abutton.frame = CGRectMake(270,0 , 40, 40);
  19. [abutton addTarget: self action: @selector(addPage:)
  20. forControlEvents: UIControlEventTouchUpInside];
  21. [containerView addSubview:abutton];
  22. }
  23. return containerView;
  24. }
  25.  
  26. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  27. if(section == 0)
  28. return 47;
  29. else {
  30. return 47-11;
  31. }
  32.  
  33. }
  34.  
  35. - (void) addPage:(id)sender {
  36. NSLog(@"addPage");
  37. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.