我有一个使用 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/