Reputation: 1358
I set all my titles´s section in my TableView.
Sometime later, i need to get the values of the title in any section´s tableview,
How can i ask that values directly to the UITableView?
Without the delegate protocol:
[self tableView:tableView titleForHeaderInSection:i]
Because this method overrides an protocol of my own class.
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
/* If datasource methdod is implemented, get´s the name of sections*/
SEL nameForSection = @selector(nameForSection:);
if ( (self.dataSource) && [self.dataSource respondsToSelector:nameForSection] )
{
NSString *titleSection = [self.dataSource nameForSection:section];
return titleSection;
}
return nil;
}
Thanks Advance...
Upvotes: 0
Views: 7869
Reputation: 11341
You can use the dataSource of the UITableView to get the section titles:
[tableView.dataSource tableView:tableView titleForHeaderInSection:section];
If you're using the default dataSource (i.e. the UITableViewController), it'll return the section title as set in InterfaceBuilder.
Here's my code that duplicates a template view and sets the title appropriately. Note that I simply use the table's header view as a convenient spot to store my template and remove it on viewDidLoad in my UITableViewController subclass.
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* header = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self.sectionHeaderTemplateView]];
UILabel* label = ((UILabel*)header.subviews[0]);
label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
return header;
}
Upvotes: 5
Reputation: 16582
Start support from iOS6.0 and higher version
You can get it like this if you have indexPath:
UITableViewHeaderFooterView* header =[self.tableView headerViewForSection:indexPath.section];
NSLog(@"Header text = %@", header.textLabel.text);
Change it like this
header.textLabel.text= @"changed";
Upvotes: 7
Reputation: 1718
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
/* If datasource methdod is implemented, get´s the name of sections*/
SEL nameForSection = @selector(nameForSection:);
if ( (self.dataSource) && [self.dataSource respondsToSelector:nameForSection] )
{
NSString *titleSection = [self.dataSource nameForSection:section];
return titleSection;
}
return nil;
Hum... Your DataSource have a DataSource ? The goal of the UITableView's dataSource is to be implemented by an other class, so self.datasource is not the datasource of UITableView !
Upvotes: 0
Reputation: 40018
This table view method will return the view for header and I think in your case it should be UILabel
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Upvotes: 1
Reputation: 1718
How did you set the sections's titles ? If you are overriding the tableView:titleForHeaderInSection: method from UITableViewDataSource, you can call it yourself to get title for a specific section. If you are overriding the tableView:viewForHeaderInSection: method from UITableViewDataSource, think about having an array containing all your labels, so that you can get label's title for a specific section.
Upvotes: 2
Reputation: 493
Store the sections title in NSArray, and use this array to get the values of any section in table view.
Upvotes: 2