Iphone 如何将值之和显示为“a”;标题「;对于分组UITableView中的每个部分?
表中的每个部分都由一个或多个包含数字的单元格组成。我想在每个章节标题中显示数字的总和 例如:Iphone 如何将值之和显示为“a”;标题「;对于分组UITableView中的每个部分?,iphone,objective-c,core-data,uitableview,Iphone,Objective C,Core Data,Uitableview,表中的每个部分都由一个或多个包含数字的单元格组成。我想在每个章节标题中显示数字的总和 例如: section title: The following sum is: 32 3 5 7 8 9 section title: The following sum is: 20 2 4 6 8 我的问题是,“我在哪里为每个部分执行计数,如何仅访问每个部分的单元格?即,哪个委托函数,等等…以便计数可以在本例程中显示 多谢各位 这是我当前的标题,标题为headerinsection委托函数 - (
section title: The following sum is: 32
3
5
7
8
9
section title: The following sum is: 20
2
4
6
8
我的问题是,“我在哪里为每个部分执行计数,如何仅访问每个部分的单元格?即,哪个委托函数,等等…以便计数可以在本例程中显示
多谢各位
这是我当前的标题,标题为headerinsection委托函数
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}
所以实际上,我的每个细胞看起来都是这样的:87.00%-(87/100)。如果我在“测验”中有8个这样的测验分数“部分,然后我想在分组的UITable中显示所有测验的平均分数,作为该部分标题的一部分。您可以使用tableView:ViewForHeaderSection:方法,并在标题中添加一个带有标签的自定义视图。标签将显示总和 我相信这会奏效的。我在我的很多应用程序中都使用过这个 代码:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *myHeader = [[UIView alloc] initWithFrame:CGRectMake(0,60,320,20)];
myHeader.backgroundColor = [UIColor blueColor];
float totpoints = //This would include the summation logic for the section;
NSString *Sum = [NSString stringWithFormat:@"%.02f",totpoints];
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0,0,80,20)] ;
myLabel1.font = [UIFont boldSystemFontOfSize:14.0];
myLabel1.textColor = [UIColor whiteColor];
myLabel1.backgroundColor = [UIColor blueColor];
myLabel1.text = Sum;
myLabel1.textAlignment = UITextAlignmentCenter;
[myHeader addSubview:myLabel1];
return myHeader;
}
我最好用节号
作为键
和数组
作为对象
的字典,然后简单地将根据节号获取的数组值相加
希望这有帮助
编辑:
另外,使用tableView:viewForHeaderInSection:
也很有好处,因为我们可以将节头放在任何我们想要的位置,尽可能使自定义视图“myHeader”上的标签“myLabel”右、左和中对齐
使用tableView:viewForHeaderInSection:
类似的方法(未测试,但接近),可以进行适当的背景色和其他细微但视觉上重要的更改
-(NSString*)表视图:(UITableView*)表视图标题标题标题部分:(NSInteger)部分
{
id sectionInfo=[[u fetchedResultsController sections]objectAtIndex:section];
float averageScoreTotal,averageScoreCount,_score,_total;
对于(Grade*S sectionInfo.objects中的Grade)
{
_分数=[sGrade.scoreValue浮动值];
_总计=[sGrade.scoreValue];
averageScoreTotal+=(\u分数/\u总数)*100;
averageScoreCount++;
}
返回[NSString stringWithFormat:@“以下总和为:%2.2f%%”,averageScoreTotal/averageScoreCount];
}
这将是最快的方法:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
NSNumber *avg=[[sectionInfo objects] valueForKey:@"@avg.scoreValue"];
NSString *title=[[sectionInfo name] stringByAppendingFormat:@":The following sum is:%@",avg];
return title;
}
-(NSString*)表视图:(UITableView*)表视图标题标题标题部分:(NSInteger)部分
{
id sectionInfo=[[u fetchedResultsController sections]objectAtIndex:section];
NSNumber*avg=[[sectionInfo对象]值forkey:@“@avg.scoreValue”];
NSString*title=[[sectionInfo name]stringByAppendingFormat:@]:以下总和为:%@,平均值];
返回标题;
}
我想您不需要访问这些单元格。如何为每个单元格分配文本?我让用户使用数据输入视图输入一系列测试分数、测验分数、项目分数。这些数据点保存在实体中,并作为核心数据管理对象的一部分进行管理。我有按类别(测验、测试等)排序的fetchedresults,我想显示每个类别的总和/平均值作为节头。试图直接从单元格访问数据不会可靠地给出正确答案。UITableView单元被重用,可用单元数等于视图中当前可见的单元数。因此,如果您有100个数据点,而UITableView一次只显示10个,那么您将只能访问10个数据点。谢谢,Sally。那么,如何确定表中每个部分存在的所有值的总和/总和呢?我正在使用带有实体等的managedObject模型。您正在使用sectionInfo.objects
获取这些数据,对吗?哇-谢谢!我必须跑步,但今晚我会尝试并发表评论@菲尔:我很高兴它帮了你:)哇,谢谢你!我必须跑步,但今晚我会尝试并发表评论!上面的代码对我来说非常有用,是对我的困境的一个很好的回答。我想我会选择viewforheaderin部分,以便控制外观。谢谢,伙计!
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
float averageScoreTotal, averageScoreCount, _score, _total;
for (Grade *sGrade in sectionInfo.objects)
{
_score = [sGrade.scoreValue floatValue];
_total = [sGrade.scorePossible floatValue];
averageScoreTotal += (_score / _total) * 100;
averageScoreCount++;
}
return [NSString stringWithFormat: @"The following sum is: %2.2f%%", averageScoreTotal / averageScoreCount];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
NSNumber *avg=[[sectionInfo objects] valueForKey:@"@avg.scoreValue"];
NSString *title=[[sectionInfo name] stringByAppendingFormat:@":The following sum is:%@",avg];
return title;
}