在UITableView中获取Section的标题

DaS*_*lva 0 iphone uitableview ipad ios

我在TableView中设置了所有标题的部分.

稍后,我需要在任何部分的tableview中获取标题的值,

我怎么能直接向UITableView询问这些值?

没有代表协议:

    [self tableView:tableView titleForHeaderInSection:i]
Run Code Online (Sandbox Code Playgroud)

因为此方法会覆盖我自己的类的协议.

- (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;
Run Code Online (Sandbox Code Playgroud)

}

谢谢提前......

Kha*_*jar 7

从iOS6.0及更高版本开始支持

如果你有indexPath,你可以这样得到它:

UITableViewHeaderFooterView* header =[self.tableView headerViewForSection:indexPath.section];
NSLog(@"Header text = %@", header.textLabel.text);
Run Code Online (Sandbox Code Playgroud)

像这样改变它

header.textLabel.text= @"changed";
Run Code Online (Sandbox Code Playgroud)


San*_*man 5

您可以使用UITableView的dataSource获取部分标题:

[tableView.dataSource tableView:tableView titleForHeaderInSection:section];
Run Code Online (Sandbox Code Playgroud)

如果您使用默认的dataSource(即UITableViewController),它将返回在InterfaceBuilder中设置的节标题。

这是我的代码,它复制模板视图并适当设置标题。请注意,我只是将表的标题视图用作方便的位置来存储模板,并将其从UITableViewController子类的viewDidLoad中删除。

- (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;
} 
Run Code Online (Sandbox Code Playgroud)