Ios 如何获取节标题视图';选择单元格时的标题
我有一个Ios 如何获取节标题视图';选择单元格时的标题,ios,cocoa-touch,uitableview,Ios,Cocoa Touch,Uitableview,我有一个UITableView,它有许多部分,每个部分只有一行 我想做的是,当我点击一个特定的单元格时,对应于该单元格的标题应该被更改 我已使用-tableView:viewForHeaderInSection: 如何在-tableView:didSelectRowAtIndexPath:方法中获取行标题?您可以使用以下方法获取节索引: indexpath.section 使用节索引,您可以简单地复制-(UIView*)tableView:(UITableView*)tableView vie
UITableView
,它有许多部分
,每个部分只有一行
我想做的是,当我点击一个特定的单元格时,对应于该单元格的标题应该被更改
我已使用-tableView:viewForHeaderInSection:
如何在
-tableView:didSelectRowAtIndexPath:
方法中获取行标题?您可以使用以下方法获取节索引:
indexpath.section
使用节索引,您可以简单地复制
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
中的内容,以获取节标题。只需将数据源用于节标题。因此,无论是什么NSArray
都可以保存
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
只需在DidSelectRowatineXpath中使用:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
NSString *titleForHeader = [myHeaderTitleArray objectAtIndex:indexPath.section];
}
您可以获得所选索引的标题视图,如:
UIView *headerView=[deviceTable headerViewForSection:indexPath.section];
然后通过循环通过.like从headerView获取孩子
for(UIView *view in headerView.subviews)
{
if ([v isKindOfClass:[UILabel class]])
{
UILabel *label=(UILabel *)v;
NSString *text=label.text;
}
}
编辑:
正如德斯德诺娃所建议的那样:
UITableViewHeaderFooterView *headerView=[deviceTable headerViewForSection:indexPath.section];
NSString *title=headerView.textLabel.text;
我建议同时实现
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];
}
Swift解决方案:
let sectionHeaderView = table.headerViewForSection(indexPath.section)
let sectionTitle = sectionHeaderView?.textLabel?.text
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
“对于所问的问题,有一个合适的方法。”德斯德诺娃-阅读问题。他问如何在didSelectRow中获取当前节标题……对于所问的问题,有一个合适的方法。@Desdenova-阅读问题。他问如何在didSelectRow中获取当前节标题…那么?获取标题视图,获取它的文本标签,它就在里面。为什么要获取视图?如果您只需要字符串值,并且在didSelectRowAtIndexPath委托中需要该字符串值,那么为什么我们要讨论viewForHeader?我根本不会使用ViewForHeaderSection(除非您需要对该标签进行一些特殊的样式设置)。99%的时间以最有效、最简单的方式做事。无需循环
headerViewForSection
返回UITableViewHeaderFooterView
。它有属性textlab
。他没有要求查看。他在didSelectRowAtIndexPath中询问标题。如果我的headerview是自定义视图,那么我如何访问我的标签text@Desdenova:是的,你是对的,但是如果你有自定义标题,你必须循环。@ElJay:他要标题。无论如何,我的代码都会给出这样的答案。有几种方法可以获得该部分的标题。如果您使用数据源(最有可能是NSArray)作为节头,那么实际上只需要一行代码就可以得到它。你读到的第一个答案不要急着回答。通读它们,确保你理解每个人在做什么。解决了我的问题,谢谢!