UITableView - 部分标题.如何更改文本? [英] UITableView - Section header. How to change text?
问题描述
我有一个使用故事板的项目.我有一个带有静态单元格和组样式的 UITableView.
I have a project which uses Storyboards. I have a UITableView with Static cells and Group style.
我需要根据分段控件(在另一部分)中所做的选择来更改一个部分中的部分文本
I need to change the section text in one section depending on which selection is made in a segmented control (in another section)
我找到了一些表明您应该使用覆盖此方法的解决方案:
<代码>- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
I have found some solutions which indicate that you should use override this method:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
并通过调用触发更新:
<代码>[[self tableView]reloadSections:[NSIndexSet indexSetWithIndex:SectionToChange] withRowAnimation:UITableViewRowAnimationFade];
问题是当我调用 reloadSections 时,相关部分中的所有行和单元格都会被删除.文本更新正确,但带有这种不必要的副作用.
The problem is when I call reloadSections then all the rows and cells in the section in question get deleted. The text updates correctly thought but with this unwanted side effect.
推荐答案
我想我在这里找到了答案:在没有 tableView:titleForHeaderInSection 的情况下更改 UITableView 部分标题
I think I found the answer here: Changing UITableView section header without tableView:titleForHeaderInSection
它可能不是很优雅,但很管用.
It may not be very elegant but it seams to work.
我可以通过调用只触发对部分标题的更新,而不会产生不需要的副作用:<代码>[self.tableView headerViewForSection:1].textLabel.text = [self tableView:self.tableView titleForHeaderInSection:1];
I can trigger an update to only the section header with none of the unwanted side effects by calling:
[self.tableView headerViewForSection:1].textLabel.text = [self tableView:self.tableView titleForHeaderInSection:1];
所以唯一需要的是实现:
So the only thing needed is to implement:
- (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;
}
这篇关于UITableView - 部分标题.如何更改文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!