When is tableView: numberOfRowsInSection: called in a UITableView?

tableView:numberOfRowsInSection sent to the UITableView delegate to find out how many rows it should have in this section.

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

+4
source share
5 answers

The method is called for the first time when the tableview becomes loaded, and if you are more interested in delegates, then we put a breakpoint and check when and where the delegate is called, and how many times.

+6
source

The following are examples of when this function will be called,

  • The first time the table is loaded
  • table data reload time
  • time to add / update / delete dynamic lines or sections.
+5
source

Method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section is the protocol method of the UITableViewDataSource protocol. It will be called the first time your table view is loaded based on the fact that you have correctly set dataSource , for example.

 self.yourTableView.dataSource = self; 

If you want to update the table again, you can call

 [self.yourTableView reloadData]; 

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

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

Hope this helps!

+3
source

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

Short answer: When your UITableView needs to update something.

Long answer:. Delegated methods are usually called themselves, but they can be called several times when your UITableView needs to update something. By default, it is called the first time the tableview is loaded or updated (reloaded).

+3
source

It depends on how often the user scrolls the UITable view in the section and how many sections it has. This is the value that is returned by this function and unloaded. The method must be canceled if you update the contents of the table view (the results of filtering or updating data through reloadData strong>).

It is best to add an entry to this function and test it yourself.

+2
source

Source: https://habr.com/ru/post/1484362/


All Articles