嗨希望有人可以提供帮助。
我目前有一个包含一组部分的表格 View ,在我的 titleForHeaderInSection 中,我返回一个字符串,该字符串包含部分单元格中包含的值的总和,以显示在部分标题中。这很好,但是当我更新单元格值时,我希望 titleForHeaderInSection 更新和刷新我的值总和。目前,用户需要将标题滚动到视线之外,然后再返回以使其刷新。我一直在谷歌搜索,看看是否能找到解决方案,看到一些示例建议在标题 View 中包含标签,但我需要这些部分是动态的,因此无法为每个部分创建标签,我也尝试使用 reloadsection 但是这也不能正常工作,并且每次在 tableview 单元格中更改值时,tableview reloaddata 都会对性能造成很大影响。
我的 titlerForHeaderInSection 当前代码是
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
int averageScoreTotal, _total;
averageScoreTotal = 0;
_total = 0;
for (BlkCon_BlockToConstructionType *sPC in sectionInfo.objects)
{
_total = [sPC.compositionPc integerValue];
averageScoreTotal += _total;
}
return [NSString stringWithFormat: @"(Total Composition for Group %d)", averageScoreTotal];
}
提前感谢您的帮助
最佳答案
你可以使用 UITableView 的 -reloadSections:...
方法与正确的部分。这也将重新加载节标题。
如果您不想使用该方法,因为您的表格 View 会停止滚动片刻或者其中一个表格 View 单元格是第一响应者,您必须为包含标签的部分使用自定义标题 View 。
1) 实现-tableView:heightForHeaderInSection:
和 -tableView:viewForHeaderInSection:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return tableView.sectionHeaderHeight;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
NSString *title = [self tableView:tableView titleForHeaderInSection:section];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, height)];
containerView.backgroundColor = tableView.backgroundColor;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(19, 7, containerView.bounds.size.width - 38, 21)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:17];
label.shadowOffset = CGSizeMake(0, 1);
label.shadowColor = [UIColor whiteColor];
label.text = title;
label.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1];
[containerView addSubview:label];
return containerView;
}
2) 通过更改其
text
直接更新标签属性(property)。您必须创建一个 iVar
用于标签或更好地使用数组来存储它们,因此当您想要更新节标题的文本时可以访问它们。3) 如果你想让标题高度灵活,设置
numberOfLines
标签的属性为 0,使其具有不定行,并确保 -tableView:heightForHeaderInSection:
返回正确的高度。为了更新节标题的高度使用
[self.tableView beginUpdates];
[self.tableView endUpdates];
祝你好运,
费边
编辑:
上面的代码假设您使用的是 ARC。
关于ios - 单元格更新后更新 TableView 节标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008265/