开发者

How to show the sum of values as a "Title" for each section in a grouped UITableView?

开发者 https://www.devze.com 2023-02-17 00:37 出处:网络
Each section in my table consists of one or more cells that contain a number.I would like to show the sum of the numbers as part of each section title.

Each section in my table consists of one or more cells that contain a number. I would like to show the sum of the numbers as part of each section title.

For example:

section title: The following sum is: 32
3
5
7
8
9 
section title: The following  sum is: 20 
2
4
6
8

My question is, "where do I perform the tally for each section and how do I access just the cells from each section? i.e. which delegate function, etc... so that the tally is ready for me to display here in this routine?

Thank you.

Here is my current TitleForHeaderInSection delegate function.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];

}

UPDATE:

Here is the code 开发者_高级运维that pulls each cell's content from the fetchedresultscontroller and places it in the cell.

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    Grade *sGrade = [_fetchedResultsController objectAtIndexPath:indexPath];

    float averageScore, _score, _total;
    _score = [sGrade.scoreValue floatValue];
    _total = [sGrade.scorePossible floatValue];
    averageScore = (_score / _total) * 100;
    cell.textLabel.text = [NSString stringWithFormat: @"%2.2f%% - (%@ of %@)", averageScore, [sGrade.scoreValue stringValue], [sGrade.scorePossible stringValue]];
    cell.detailTextLabel.text = [sGrade commentInfo];
}

So in reality, each of my cells look something like this: 87.00% - (87 / 100). And so if I have 8 quiz scores like this in the "quizzes" section, then I want to show the average score for all quizzes as part of the title for that section in the grouped UITable.


You can use tableView:viewForHeaderInSection: Method and add a custom view in header with a label in it. The label will display the sum.

I am sure this would work. I have used this in many of my apps.

Code:

- (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;
}

I would better go for a dictionary with section number as keys and arrays as objects and then simply I have to sum up the array values fetched based on section number.

Hope this helps.

EDIT:

Also using tableView:viewForHeaderInSection: is beneficial because we can put the section header in any position we want as far as right,left and center alignment of the label "myLabel" on the custom View "myHeader" is concerned.

And putting proper background color and other minor but visually important changes can be made using tableView:viewForHeaderInSection:


Something like this (untested, but close)

- (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];
}


This would be the quickest way:

- (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;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号