brianhevans
brianhevans

Reputation: 1193

get the title of a tableview titleForHeaderInSection

I have 4 sectioned tables in my iOS 6 app. These tables each have a title that I set in the titleForHeaderInSection. I would like to know how I can access this title with NSLog in didSelectRowAtIndexPath. I DO see the string value of the row I clicked on in the alert but I would also like the title of the tableview section. I'm not sure how to get that.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:indexPath.section];

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;

NSLog(@"Selected Cell: %@", cellText);

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Selected a row" message:[sectionArray objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}


- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

NSString *result = nil;

if ([tableView isEqual:self.myTableView] && section == 0) {

    myTableView.tableHeaderView.tag = FROZEN;
    result = @"Frozen";

} else if ([tableView isEqual:self.myTableView] && section == 1) {

    myTableView.tableHeaderView.tag = FRUIT;
    result = @"Fruit";

}
else if ([tableView isEqual:self.myTableView] && section == 2) {

    myTableView.tableHeaderView.tag = SALADS;
    result = @"Salads";

} else if ([tableView isEqual:self.myTableView] && section == 3) {

    myTableView.tableHeaderView.tag = VEGETABLES;
    result = @"Vegetables";
}

return result;
}

Upvotes: 1

Views: 3583

Answers (2)

iDev
iDev

Reputation: 23278

Store the titles of sections in an array as,

NSArray *sectionTitles = [NSArray arrayWithObjects:@"Frozen", @"Fruit", @"Salads", @"Vegetables", nil];

And modify your titleForHeaderInSection method as,

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *result = [sectionTitles objectAtIndex:section];
//....
return result;
}

Modify didSelectRowAtIndexPath as,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//...
NSLog(@"Header title: %@",  [sectionTitles objectAtIndex:indexPath.section]);
//...
}

Another option is to use the below method in didSelectRowAtIndexPath

NSLog(@"Header title: %@",  [self tableView:tableView titleForHeaderInSection:indexPath.section]);

Upvotes: 3

user1078170
user1078170

Reputation:

Well, it does seem like UITableView should provide access to this for you, but I'm not finding anything... The easiest way to implement this would be, I think, to create an array (I'll call it mySectionTitles and assume it's a property) with your section titles and then, in didSelectRowAtIndexPath, call [self.mySectionTitles objectAtIndex:indexPath.section] and do whatever you want with the returned string.

Upvotes: 1

Related Questions