[英]UITableView - Section header. How to change text?
I have a project which uses Storyboards. 我有一个使用Storyboard的项目。 I have a UITableView with Static cells and Group style.
我有一个带静态单元格和组样式的UITableView。
I need to change the section text in one section depending on which selection is made in a segmented control (in another section) 我需要在一个部分中更改部分文本,具体取决于在分段控件中进行的选择(在另一部分中)
I have found some solutions which indicate that you should use override this method: 我找到了一些解决方案,表明你应该使用覆盖这个方法:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
and trigger an update by calling: 并通过调用触发更新:
[[self tableView]reloadSections:[NSIndexSet indexSetWithIndex:SectionToChange] withRowAnimation:UITableViewRowAnimationFade];
The problem is when I call reloadSections then all the rows and cells in the section in question get deleted. 问题是当我调用reloadSections时,相关部分中的所有行和单元格都会被删除。 The text updates correctly thought but with this unwanted side effect.
文本更新正确,但有这种不必要的副作用。
I think I found the answer here: Changing UITableView section header without tableView:titleForHeaderInSection 我想我在这里找到了答案: 更改UITableView节标题而不使用tableView:titleForHeaderInSection
It may not be very elegant but it seams to work. 它可能不是很优雅,但接缝工作。
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];
我可以通过调用:
[self.tableView headerViewForSection:1].textLabel.text = [self tableView:self.tableView titleForHeaderInSection:1];
只触发更新到没有不需要的副作用的节头[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;
}
If you have implemented these functions then removing them should fix your problem: 如果您已实现这些功能,那么删除它们应该可以解决您的问题:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{ if (section == 0)
{
lbl.text = @"abc";
}
if (section == 2)
{
lbl.text = @"2ndsectionbeigns";
}
return headerview;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.