选择单元格时如何获取节标题视图的标题

iDi*_*Dia 8 cocoa-touch uitableview ios

我有一个UITableView有很多sections,每个部分只有一行.

我想要做的是,当我点击特定单元格时,应该更改与单元格对应的标题的标题.

我已经设置了节标题 -tableView:viewForHeaderInSection:

如何在-tableView:didSelectRowAtIndexPath:方法中获取行标题?

V-X*_*eme 7

您可以获取所选索引的标题视图,如:

UIView *headerView=[deviceTable headerViewForSection:indexPath.section];
Run Code Online (Sandbox Code Playgroud)

然后通过循环遍历从headerView中获取子项

for(UIView *view in headerView.subviews)
{
     if ([v isKindOfClass:[UILabel class]])
        {
            UILabel *label=(UILabel *)v;
            NSString *text=label.text;
        }
}
Run Code Online (Sandbox Code Playgroud)

编辑: 正如Desdenova所说:

    UITableViewHeaderFooterView *headerView=[deviceTable headerViewForSection:indexPath.section];
    NSString *title=headerView.textLabel.text;
Run Code Online (Sandbox Code Playgroud)


jsz*_*ski 4

我建议同时实现tableView:titleForHeaderInSection:tableView:viewForHeaderInSection:(如果两者都实现,那么 iOS 更喜欢viewForHeaderInSection:)。然后让您的实现tableView:viewForHeaderInSection:使用标签创建其视图并使用以下结果填充它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;
}
Run Code Online (Sandbox Code Playgroud)

现在,当您响应行点击时,很容易获得相应的标题:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     NSString *titleForHeader = [self tableView:tableView titleForHeaderInSection:indexPath.section];
}
Run Code Online (Sandbox Code Playgroud)