Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should have a proper data model. For example, if you have 30 cells, there is no need to display all the 30 cells in your tableview. Doing that will be a waste of resources and that why we always implement dequereuseable method in tableview delegates.</p> <p>Having said that, do something like this</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface ViewController : UITableViewController @end </code></pre> <p>Implementation as below</p> <pre><code> #import "ViewController.h" @interface ViewController () { NSArray *numbersList; } -(float) calculateAvg; @end @implementation ViewController - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; numbersList = [[NSArray alloc]initWithObjects:@"10",@"23",@"34",@"43",@"57",@"64",@"77",@"88",@"95",nil]; [[self tableView] setDelegate:self]; [[self tableView] setDataSource:self]; } </code></pre> <p>#pragma mark - Table view data source</p> <pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. Plus one to hold average return [numbersList count] + 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if(cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } if([indexPath row] &lt; [numbersList count]) { [[cell textLabel] setText:[numbersList objectAtIndex:[indexPath row]]]; } else { float avg = [self calculateAvg]; [[cell textLabel] setText:[NSString stringWithFormat:@"%f",avg]]; } return cell; } -(float) calculateAvg { float avg = 0; for(int idx=0;idx&lt;[numbersList count];idx++) { int tempValue = [[numbersList objectAtIndex:idx] intValue]; avg = avg + tempValue; } return (avg / [numbersList count]); } </code></pre>
 

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