16

Today my question is about UITableViewController-s In particular I have noticed that the datasource delegate method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

is called twice (even if for instance I just create a navigation based application and without adding a line of code.. well adding an NSLog to track it). Now, since in my application I need to determine the number of sections basing the choice on the documents in the file system, I need to call some methods to do so. I have put these methods in the above mentioned method, so they will be called twice, which is something I don't need. The questions are why is it called twice, can I have it called once? I hope that in the official documentation this is not clearly stated (which would mean that I didn't read it at all :) ). By the way I could see others posting similar questions, but I couldn't find a fully satisfying answer. Thank you.

user236739
  • 1,323
  • 2
  • 15
  • 21
  • 1
    did you ever find out why this is? Happening to me also. Exactly the same... – Remover Dec 05 '10 at 02:45
  • I am also experiencing this behavior on ios 4.2 in xcode 4.1. I can't seem to identify where that extra call comes from, has anybody found a solution yet? Or is this a bug? – Mellson Feb 28 '11 at 12:10
  • I am also seeing the same issue (whereby numberOfSectionsInTableView is called twice in certain circumstances). This is with iOS 4.2. – kennethmac2000 Jan 29 '11 at 23:58
  • only - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; method is called twice or all delegate method of uitableview are called twice? If all are called twice that means ur table is reloading twice... Are calling the "reload" method of uitableview? – Reena Apr 15 '10 at 10:02
  • Hello Reena, thank yo for your attention. Only - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; is called twice. All the other datasource delegate methods are called as many times as you would expect. For instance - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { is called n = (number of visible cells) and it is limited to (number of sections x number of rows per section), each time you scroll and get some new cell visible, it's called. Th only apparently inexplicable double call is for number of sections method... – user236739 Apr 15 '10 at 10:36
  • Can u post the code for numberOfSectionsInTableView method? – Reena Apr 15 '10 at 10:45
  • Well...of course, but I can tell you there are just to staments: an NSLog statement and 'return 3;' (no matter the number, same behavior). In the meanwhile I discovered that the issue is there only with UITableViewController, when using just UITableView the method gets called just once as expected. – user236739 Apr 16 '10 at 10:18

4 Answers4

14

I was experiencing the same problem, only with the call to numberOfRowsInSection: The answered laid in the stack trace for each call I received.

  1. The first call was due to a change in the table header view I was making in the viewDidLoad: of my viewcontroller.

    thumbView.tableHeaderView = nil;
    thumbView.tableFooterView = nil;
    

    This resulted in internal call to _updateContentSize: which called heightForTable: which eventually called numberOfRowsInSection:. This was something I triggered, and could be easily avoided by not doing the above code :)

  2. The second call was the legitimate one in order to reloadData. This is triggered by a layout event somewhere and most likely you can't skip it.

I'm sure you can observe something similar for the numberOfSections: method

So, my conclusion is that due to the the implementation of UITableView there are many situations where certain delegate methods will get called twice or more because the table view has to refresh something. I try to design my code around this bug/feature/etc.

Hope it helps

Andrei Stanescu
  • 6,353
  • 4
  • 35
  • 64
  • if i set or not set -> thumbView.tableHeaderView = nil; thumbView.tableFooterView = nil; "numberOfSectionsInTableView" called 5 times. Why ? – Bhargav B. Bajani Jun 01 '16 at 13:10
2

If your tableview is contained by a child view controller, Try this at your parent ViewController

[parentViewController addChildViewController:childViewController];

before [parentViewController.view addSubview:childViewController.view]

Wanbok Choi
  • 5,432
  • 1
  • 21
  • 26
0

Please check your code, after adding TableView you may again called realodData method of table in mey be ViewWillAppear method

Madhu
  • 1,542
  • 1
  • 14
  • 30
0

This can happen if you'r table view's frame gets changed by mistake in the story board.Say you clicked on the storyboard where you have added the table view as a subview and now your table may not be having the proper frame which you have set in the beginning.