UITableView scrolling and memory problems

Discussion in 'iOS Programming' started by depl0y, Feb 5, 2009.

  1. depl0y macrumors newbie

    Joined:
    Sep 20, 2008
    #1
    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.
     
  2. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    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?
     
  3. depl0y thread starter macrumors newbie

    Joined:
    Sep 20, 2008
    #3
    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!
     
  4. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #4
    Also, check you're autoreleasing the cell if you do have to create one....
     

Share This Page