Return to Snippet

Revision: 31729
at September 11, 2010 05:36 by gerhardsletten


Updated Code
- (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");
}

Revision: 31728
at September 11, 2010 05:31 by gerhardsletten


Initial Code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if(section == 0) {
		return 1;
	} else {
		return 5;
	}
}

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

- (void) addPage:(id)sender {
	NSLog(@"addPage");
}

Initial URL

                                

Initial Description
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.

Initial Title
Recreate the default section headers in Rounded UITableview

Initial Tags
iphone

Initial Language
Objective C