Monday, October 13, 2008

iPhone Code - Table Header Context Help

Now that the iPhone NDA has been changed, I thought I'd post a bit of code for various things I've done for the iPhone. I wanted to do a contextual help sort of thing for different sections in a grouped table view. This will be in the next version of FTP On The Go and shows some of the features coming if you look closely at the screen :)
Pressing the button pushes a view onto the navigation controller that shows a webpage off our server with the help contents.

I got the graphic for the help button by changing the hue/saturation from an icon purchased from icons-icons.com.
Sorry for bad formatting from the blog stuff.


In the .h file...

#define SETTING_HEADER_FONT_SIZE 16.0
#define SETTING_HEADER_HEIGHT 36.0
#define SETTING_HEADER_ROW_WIDTH 308.0

In the .m file...


In the viewDidLoad, there's this to set the heights.

tableView.sectionHeaderHeight = SETTING_HEADER_HEIGHT;
tableView.sectionFooterHeight = 6;


- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section {
UIView *hdrView = [[UIView alloc]
initWithFrame:CGRectMake(0,6, SETTING_HEADER_ROW_WIDTH, SETTING_HEADER_HEIGHT)];
hdrView.backgroundColor = [UIColor clearColor];
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(12, 6, SETTING_HEADER_ROW_WIDTH-100, SETTING_HEADER_HEIGHT)];
label.font = [UIFont boldSystemFontOfSize:SETTING_HEADER_FONT_SIZE];
label.textAlignment = UITextAlignmentLeft;
label.highlightedTextColor = [UIColor whiteColor];
label.textColor = [UIColor darkGrayColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0, 1);
label.backgroundColor = [UIColor clearColor];

if (section==0) {
label.text = @"Downloading Files";
} else if (section==1) {
label.text = @"Sharing \"Saved Files\"";
}
[hdrView addSubview:label];

UIButton *button = nil;
if (section==1) {
button = [[UIButton alloc] initWithFrame:CGRectMake(SETTING_HEADER_ROW_WIDTH-32, 2, 32, 32)];
[button addTarget:self action:@selector(clickSharingHelp: forControlEvents:UIControlEventTouchUpInside];
}

if (button!=nil) {
[button setImage:[UIImage imageNamed:@"help32.png"] forState:UIControlStateNormal];
button.showsTouchWhenHighlighted = YES;
[hdrView addSubview:button];
}
return hdrView;
}

2 comments:

smith288 said...

Im assuming this needs to be in grouped style for the sections to not stay static while the table moves up and down? If it can be a plain styled table, then any idea why my sections stay put as the table moves?

Michael said...

Yes, for grouped style. Haven't tried with ungrouped...