ios - UITableView - 部分标题。如何更改文本?

标签 ios objective-c uitableview

我有一个使用 Storyboard的项目。我有一个带有静态单元格和组样式的 UITableView。

我需要根据在分段控件(在另一部分)中做出的选择更改一个部分中的部分文本

我找到了一些解决方案,表明您应该使用重写此方法:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

并通过调用触发更新:
[[self tableView]reloadSections:[NSIndexSet indexSetWithIndex:SectionToChange] withRowAnimation:UITableViewRowAnimationFade];

问题是当我调用 reloadSections 时,相关部分中的所有行和单元格都会被删除。文本更新正确,但有这种不需要的副作用。

最佳答案

我想我在这里找到了答案:Changing UITableView section header without tableView:titleForHeaderInSection

它可能不是很优雅,但可以正常工作。

我可以通过调用以下命令只触发对节标题的更新而没有任何不需要的副作用: [self.tableView headerViewForSection:1].textLabel.text = [self tableView:self.tableView titleForHeaderInSection:1];

所以唯一需要做的就是实现:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   if (section == 1){
      if (self.segmentX.selectedSegmentIndex == 0) // or some condition
         return @"Header one";
      else
         return @"Header two";
  }
  return nil;
}

关于ios - UITableView - 部分标题。如何更改文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19350124/

相关文章:

ios - 栈溢出错误,没有递归

ios - 为什么我不能更改 UIButton 文本颜色?

ios - 在没有 UIWebView 的 iOS 设备上显示 .RTF 文件 - iOS 上的 initWithRTF?

iphone - map 套件 :How to add annotation on MKLocalSearch results placemarks?

将默认数据库路径更改为组路径后,iOS RealmSwift 不会更新模型

iOS 通用链接 NSURLAuthenticationMethodServerTrust kAuthenticationErr

objective-c - 解析维基百科表格

ios - 如何使用 Objective C 在 Realm DB 中存储数组值 (RLMArray)?

objective-c - 更改UISearchBar iOS 7 的字体大小和字体样式

ios - 如何始终在 Obj c 中看到 Tableview 的滚动条?