Reputation: 1397
I have an UITableView
which has many sections
, and each section has only 1 row.
What I want to do is that, when I click on a particular cell, the title of the header that corresponds to the cell should be changed.
I have set the section header using -tableView:viewForHeaderInSection:
How can I get the row header title inside the -tableView:didSelectRowAtIndexPath:
method?
Upvotes: 8
Views: 19840
Reputation: 35412
Swift 4:
let indexPath = IndexPath.init(row: 0, section: 0) // put here your row or section number..
let myHeaderView = tableView.headerView(forSection:indexPath.section)
let title = myHeaderView?.textLabel?.text
Upvotes: 0
Reputation: 5846
Swift solution:
let sectionHeaderView = table.headerViewForSection(indexPath.section)
let sectionTitle = sectionHeaderView?.textLabel?.text
Upvotes: 4
Reputation: 7416
I would suggest implementing both tableView:titleForHeaderInSection:
and tableView:viewForHeaderInSection:
(if both are implemented then iOS prefers viewForHeaderInSection:
). Then have your implementation of tableView:viewForHeaderInSection:
create its view with the label and populate it with the result of tableView:titleForHeaderInSection:
:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *yourHeaderView;
UILabel *someLabel;
// set up the view + label
// if self doesn't implement UITableViewDelegate, you can use tableView.delegate
someLabel.text = [self tableView:tableView titleForHeaderInSection:section];
return yourHeaderView;
}
Now when you're responding to a row tap, it's very easy to get the corresponding title:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *titleForHeader = [self tableView:tableView titleForHeaderInSection:indexPath.section];
}
Upvotes: 4
Reputation: 7333
You can get the header view for selected Index like :
UIView *headerView=[deviceTable headerViewForSection:indexPath.section];
Then get the child from the headerView by looping through.like
for(UIView *view in headerView.subviews)
{
if ([v isKindOfClass:[UILabel class]])
{
UILabel *label=(UILabel *)v;
NSString *text=label.text;
}
}
Edit: As Desdenova suggested :
UITableViewHeaderFooterView *headerView=[deviceTable headerViewForSection:indexPath.section];
NSString *title=headerView.textLabel.text;
Upvotes: 7
Reputation: 14427
Just use the datasource for the section header. So whatever NSArray
you have that hold the string values for the
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
just use that in the didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
NSString *titleForHeader = [myHeaderTitleArray objectAtIndex:indexPath.section];
}
Upvotes: 0
Reputation: 379
You can get the section index by using:
indexpath.section
With the section index, you can simply duplicate what you have in -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
to get the section title.
Upvotes: -1