Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use <strong>tableView:viewForHeaderInSection:</strong> Method and add a custom view in header with a label in it. The label will display the sum.</p> <p>I am sure this would work. I have used this in many of my apps.</p> <p><strong>Code:</strong> </p> <pre><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; } </code></pre> <p>I would better go for a dictionary with <code>section number</code> as <code>keys</code> and <code>arrays</code> as <code>objects</code> and then simply I have to sum up the array values fetched based on section number.</p> <p>Hope this helps.</p> <p><strong>EDIT:</strong></p> <p>Also using <code>tableView:viewForHeaderInSection:</code> 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.</p> <p>And putting proper background color and other minor but visually important changes can be made using <code>tableView:viewForHeaderInSection:</code> </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload