Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

depl0y

macrumors newbie
Original poster
Sep 20, 2008
10
0
I have a problem with a UITableView in my application. I think the base of the problem is memory usage. I will try to explain my situation below:

I have a couple of classes, for example:

book.h
Code:
NSString* name;
NSString* description
NSMutableArray* chapters;

chapter.h
Code:
NSString* title;
NSMutableArray* paragraphs;

paragraph.h
Code:
NSString* title;
NSString* text;
NSString* author;


When I load my book, it instantiates a X number of chapters, which also instantiates an Y number of paragraphs. The chapters are added to the NSMutableArray "chapters" of book and the paragraphs I load are added to the NSMutableArray "paragraphs" from the class "chapter". Well, this loading is no problem and this seem to work with no problems.

Now I want to fill an UITableView with all the chapters of a certain book as sections and add the paragraph titles in those sections as normal rows. I do this with the normal techniques (this is just code I made up, don't have my code with my right now, so syntax errors should be ignored, because the actually compiles and runs, etc).

Code:
book* currentBook = [[book alloc] init];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     return [currentBook.chapters count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
     chapter* sectionChapter = [currentBook.chapters objectAtIndex:section];
     return sectionChapter.title;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     chapter* sectionChapter = [currentBook.chapters objectAtIndex:section];
     return [sectionChapter.paragraphs count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
     chapter* sectionChapter = [currentBook.chapters objectAtIndex:indexPath.section];
     paragraph* currentParagraph = [sectionChapter.paragraphs objectAtIndex:indexPath.row];
 
     // instantiate cell etc
 
     cell.text = currentParagraph.title;
 
     // return cell etc..
}

Now, when I scroll through my list of chapters (sections) and paragraphs (rows) the application slows down to a halt after a 100 or so. After that everything just hangs and I actually have to restart my phone to get it working again. In the simulator this runs just fine, but I think that is because the simulator has no memory limit (except maybe the limit from my MacBook pro).

What is a good way to get this working? Should I be extracting the information I need in my table to for example a NSDictionary and use that to create sections and rows?

Some help would be greatly appreciated.
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
There's nothing wrong there as far as I can see. My guess is that there's something going on in the bits you've commented out in the cellForRowAtIndexPath: method. Are you reusing cells like you're supposed to?
 

depl0y

macrumors newbie
Original poster
Sep 20, 2008
10
0
There's nothing wrong there as far as I can see. My guess is that there's something going on in the bits you've commented out in the cellForRowAtIndexPath: method. Are you reusing cells like you're supposed to?

OMG, how could I be so blind? Sorry about this :(. Yup, that actually did the job. And this is not even the first application I make that uses a UITableView. Thanks a lot for your answer!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.