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
chapter.h
paragraph.h
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).
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.
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.