When is tableView:numberOfRowsInSection: called in UITableView?

| | August 18, 2015

tableView:numberOfRowsInSection is sent to the delegate of a UITableView to find out how many rows it needs to have in a given section.

My question is, when and how often is this method called?

5 Responses to “When is tableView:numberOfRowsInSection: called in UITableView?”

  1. Below are the instances when that function will get called,

    1. For the first time when table is loaded
    2. the time you reload the table data
    3. the time you add/update/delete your row or sections dynamically.
  2. My question is, when and how often is this method called?

    Short Answer : When your UITableView needs to update something.

    Long Answer : Delegates Methods generally called themselves however it may be called multiple times when your UITableView needs to update something. By default, it’s called very first time the tableview is getting loaded or updated (reloaded).

  3. The method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section is a protocol method of the UITableViewDataSource – protocol. It will be called
    the very first time your table view is loaded based on that you have set the dataSource properly, e.g.

    self.yourTableView.dataSource = self;
    

    If you are interested in updating your table again at a later time you can call

    [self.yourTableView reloadData];
    

    in order to reload the entire table. If you are only interested in reloading a part of your table you can do something similar to

    NSIndexSet *reloadSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self numberOfSectionsInTableView:self.yourTableView])];
    [self.yourTableView reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationAutomatic];
    

    Hope it helps!

  4. It depends on how often user will scroll UITable view to section and how many sections there are. This value, which is returned by this function and is casched. Method will need be revoked if you will update content of table view (filtering results, or updating data via reloadData).

    Best thing for you will be to add logging to this function and check this yourself.

  5. The method is called very first time the tableview is getting loaded and if you are more interested in the delegates then put a breakpoint and check when and where which delegate is called and how many times.

Leave a Reply