嗨,希望有人能帮上忙。
我当前有一个包含一组节的表视图,在我的titleForHeaderInSection中,我返回了一个字符串,其中包含要在节标题中显示的节单元格中包含的值的总和。这很好,但是当我更新一个单元格值时,我希望titleForHeaderInSection更新并刷新我的值的总和。此时,用户需要将标题滚动到看不见的地方,然后再返回以刷新标题。我一直在谷歌搜索,看看是否可以找到一个解决方案,我看到了一些例子,建议在视图中包括一个标题的标签,但我需要的部分是动态的,所以不能为每个部分创建标签,我也已经尝试使用reload节,但这也不能正常工作,表视图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];}
提前感谢您的帮助
发布于 2012-08-18 00:23:39
您可以对正确的部分使用UITableView的-reloadSections:...方法。这也将重新加载节标题。
如果您不想使用该方法,因为您的表格视图停止滚动片刻,或者其中一个表格视图单元格是第一响应者,则必须为包含标签的部分使用自定义标题视图。
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属性直接更新标签。您必须为标签创建一个iVar,或者最好使用数组来存储它们,这样当您想要更新节标题的文本时就可以访问它们。
3)如果要使页眉高度灵活,请将标签的numberOfLines属性设置为0,使其具有不确定的行,并确保-tableView:heightForHeaderInSection:返回正确的高度。
要更新节标题的高度,请使用
[self.tableView beginUpdates];
[self.tableView endUpdates];祝好运,
Fabian
编辑:
上面的代码假设你使用的是ARC。
https://stackoverflow.com/questions/12008265
复制相似问题