[英]UITableView - Section header. How to change text?
我有一个使用Storyboard的项目。 我有一个带静态单元格和组样式的UITableView。
我需要在一个部分中更改部分文本,具体取决于在分段控件中进行的选择(在另一部分中)
我找到了一些解决方案,表明你应该使用覆盖这个方法:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
并通过调用触发更新:
[[self tableView]reloadSections:[NSIndexSet indexSetWithIndex:SectionToChange] withRowAnimation:UITableViewRowAnimationFade];
问题是当我调用reloadSections时,相关部分中的所有行和单元格都会被删除。 文本更新正确,但有这种不必要的副作用。
我想我在这里找到了答案: 更改UITableView节标题而不使用tableView:titleForHeaderInSection
它可能不是很优雅,但接缝工作。
我可以通过调用: [self.tableView headerViewForSection:1].textLabel.text = [self tableView:self.tableView titleForHeaderInSection:1];
只触发更新到没有不需要的副作用的节头[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;
}
如果您已实现这些功能,那么删除它们应该可以解决您的问题:
- (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.