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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...