当选中一个单元格时,如何获取节标题视图的标题

8

我有一个UITableView,它有许多sections,每个section只有1行。

我想要实现的是,当我点击某个cell时,与之对应的header标题应该被更改。

我已经使用-tableView:viewForHeaderInSection:来设置了section header。

-tableView:didSelectRowAtIndexPath:方法中,我该如何获取行头标题呢?


有几种方法可以获取该部分的标题。如果您正在使用数据源(最可能是NSArray)作为部分标题,那么您只需要一行代码即可获取它。不要轻易采纳您读到的第一个答案。请仔细阅读所有答案,并确保您理解每个答案的含义。 - LJ Wilson
6个回答

7
您可以按以下方式获取所选索引的标题视图:
UIView *headerView=[deviceTable headerViewForSection:indexPath.section];

然后通过循环遍历从headerView中获取子项。例如:
for(UIView *view in headerView.subviews)
{
     if ([v isKindOfClass:[UILabel class]])
        {
            UILabel *label=(UILabel *)v;
            NSString *text=label.text;
        }
}

编辑: 正如Desdenova所建议的:

    UITableViewHeaderFooterView *headerView=[deviceTable headerViewForSection:indexPath.section];
    NSString *title=headerView.textLabel.text;

@ElJay:他要求标题头。我的代码无论如何都会给出那个。 - V-Xtreme
是的,它可以实现,但效率远不如直接从用于填充标题的数据源中获取标题。此外,OP要求在didSelectRowAtIndexPath中获取该值。您的答案使用了多行代码和循环来获取它。我的答案只使用一行代码且没有循环。 - LJ Wilson
@saji723:你的自定义视图里有哪些元素? - V-Xtreme
@ElJay:你也在使用数组。如果这不是一个自定义标题,那么我的代码也只有两行。请看我更新后的答案。对于自定义标题,在你的情况下有什么规定呢?可能你也需要迭代处理。 - V-Xtreme
@ElJay:不是问题的一部分。但请看下面我的回答评论区。他有一个自定义标签。 - V-Xtreme
显示剩余6条评论

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;
}

现在当您响应行选中时,获取相应的标题非常容易:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     NSString *titleForHeader = [self tableView:tableView titleForHeaderInSection:indexPath.section];
}

解决了我的问题,谢谢! - Hassy

4

快速解决方案:

let sectionHeaderView = table.headerViewForSection(indexPath.section)
let sectionTitle = sectionHeaderView?.textLabel?.text

0

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

0

只需使用数据源作为部分标题。因此,无论您拥有的NSArray中包含哪些字符串值

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

只需在didSelectRowAtIndexPath中使用它:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
NSString *titleForHeader = [myHeaderTitleArray objectAtIndex:indexPath.section];
}

1
@Desdenova - 请认真阅读问题。他问的是如何在didSelectRow中获取当前章节标题。 - LJ Wilson
那么?获取头视图,获取其文本标签并将其放在其中。 - Desdenova
在编程中,特别是在iOS中,你不会得到你不需要的东西。始终采取最短的路径来获取所需内容。资源和内存是有限的,你需要尽可能地高效。虽然获取视图,然后深入该视图以获取标签,然后获取该标签的文本属性并不是一个巨大的内存问题,但这样做的想法是错误的,因为你可以通过获取已经存在于数据源中的NSString对象的值来获得相同的结果。 - LJ Wilson
是的,我会的。如果你只需要一个标签,只需使用该数组和titleForHeaderInSection方法来填充该标签。 - LJ Wilson
1
99%的情况下,尽可能以最高效和最简单的方式完成任务,除非你需要对该标签进行某些特殊的样式设置,否则不要使用viewForHeaderInSection。 - LJ Wilson
显示剩余5条评论

-1

您可以使用以下代码获取部分索引:

indexpath.section

有了章节索引,你可以简单地复制在-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section中的内容以获取章节标题。


有一个适当的方法可以解决所询问的问题。 - Desdenova
@Desdenova - 请仔细阅读问题。他询问如何在didSelectRow中获取当前部分标题的名称... - LJ Wilson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接