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

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

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

我使用-tableView:viewForHeaderInSection设置了节头:

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

解决方法

我建议实现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];
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...