12

How many times should the tableView:numberOfRowsInSection method be called when populating a table view?

My application is crashing with no warnings, errors, or a stack trace. I have also tested for memory leaks and have found none. The app holds steady at about 1.4MB.

I have NSLog reporting as methods are messaged, and I notice that tableView:numberOfRowsInSection is being called multiple times. The app crashes during one of these "extra" calls. The point at which the app crashes varies. I should point out that the table is populating about 600 cells, if that makes a difference.

I can post code if you want, but my data source comes from a singleton class, so It is a lot of code. Any help would be greatly appreciated.

Robin
  • 10,011
  • 5
  • 49
  • 75
Chris
  • 4,762
  • 3
  • 44
  • 79

3 Answers3

1

I faced the same problem, the first time tableview instance is not nil and the subsequent times it is nil. The problem was, I had yet not set the return value for

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

and because of this it kept happening. Once I set a return value the problem was gone. Hope this helps someone.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
Omar
  • 463
  • 3
  • 11
1

See my answer in another similar question: link
Basically, calls to the the delegate methods of a table view can be triggered by different events. It wouldn't be a suprise to me that UITableView calls numberOfRowsInSection multiple times (even for the same section).

Community
  • 1
  • 1
Andrei Stanescu
  • 6,353
  • 4
  • 35
  • 64
0

the best way is to use Lazy table image loading . so that the 600 cells is a big amount and if you call all the data in a single time it will always going to be crashed due to memory issues.

try using the code I provided .

Another answer on the stackoverflow can help you out.

Community
  • 1
  • 1
V.V
  • 3,082
  • 7
  • 49
  • 83
  • Im not loading images, just text. Can the Lazy Table image loading method work for that as well? – Chris Mar 08 '11 at 20:20
  • It should work for you. as with lazy table image you can load a number of images along with text... so try and change something in the lazy table images code , make it according to your needs ... you will definitely get the answer.. – V.V Mar 09 '11 at 04:04
  • Is there a certain amount of cells which would be supported? I am loading this info via a JSON string, so I get all 600+ cells of data in one call. Should I only display 50 at a time with a load more option? And if the user eventually loads all 600 is that going to be an issue? Or is there some kind of pagination technique I should be using? – Chris Mar 09 '11 at 18:47
  • That would be a better option, you can put a button at the end of 50 data , press to show more and it should solve your problem. – V.V Mar 10 '11 at 03:48
  • I'm confused, because the way the UITable rendering works, it only renders enough cells to fill the screen. The rest are rendered during scroll. So What difference does it make if I have 50 cells or 1000? – Chris Apr 04 '11 at 18:45