-1

In this app, I download data from Parse. That data is a list of 5 Messages, each with a category. There are 4 different categories as two of the messages have the same category. I want to put that data into sections on a tableview. Since the data is not ready for sectioning, I had to create 2 mutable arrays that act like index look ups. (followed this guide: https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections)

Problem: I'm getting this error: -[__NSArrayM objectAtIndex:]: index 8 beyond bounds [0 .. 4]'

Question is, why am I getting this error and how do I fix it?

I've located the exact line that is the problem. First, here is what you need.

1st Mutable Dictionary: self.sectionToCategoryMap //This property maps the sections titles to the row indeces for the data. The output it looks like this: (read as, get the 1st category header from object at index 0)

0 = "Section Header 1"; 
24 = "Section Header 2";
16 = "Section Header 3";
32 = "Section Header 4";

2nd Mutable Dictionary: self.sections // This maps what items are in what section(category). Output looks like this:

"category 1" =(32);
"category 2" =(24);
"category 3" =(16);
"category 4" =(0,8);

These two Dictionaries are created by this code:

- (void)prepSections:(id)array {
    [self.sections removeAllObjects];
    [self.sectionToCategoryMap removeAllObjects];
    self.sections = [NSMutableDictionary dictionary];
    self.sectionToCategoryMap = [NSMutableDictionary dictionary];

    NSInteger *section = 0;
    NSInteger *rowIndex = 0;
    for (MessageItem  *messageItem in self.messageList) {
        NSString *category = [messageItem valueForKey:@"messageCategory"]; //retrieves category for each message -1st regulator
        NSMutableArray *objectsInSection = [self.sections objectForKey:category]; //assigns objectsinsection value of sections for current category
        if (!objectsInSection) {
             objectsInSection = [NSMutableArray array];
            // this is the first time we see this category - increment the section index
            //literally it ends up (0:Regulatory)
            [self.sectionToCategoryMap setObject:category forKey:[NSNumber numberWithInt:rowIndex]];
            section++;
        }
        [objectsInSection addObject:[NSNumber numberWithInt:(int)rowIndex++]]; //adds message to objects in section
        [self.sections setObject:objectsInSection forKey:category]; //adds dict of objects for category
    }
}

The error is happening in my in cellForRowAtIndexPath below, specifically the line: NSNumber *rowIndex = [rowIndecesInSection objectAtIndex:indexPath.row]; (note: categoryForSection is a helper method I defined, its implementation is also below.)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSString *category= [self categoryForSection:indexPath.section];
    NSArray *rowIndecesInSection = [self.sections objectForKey:category];
    NSNumber *rowIndex = [rowIndecesInSection objectAtIndex:indexPath.row]; //pulling the row indece from array above
    //gets to 3 and breaks!!!

    messageItem = [self.messageList objectAtIndex:[rowIndex intValue]];
    [cell configMessageCell:messageItem indexPath:indexPath];
    return cell;
}

For good measure, here is the rest of my code.

- (NSString *) categoryForSection:(NSInteger*)section { //takes section # and returns name of section.
    return [self.sectionToCategoryMap objectForKey:[NSNumber numberWithInt:(int)section]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return (unsigned long)self.sections.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *category = [self categoryForSection:section];
    NSArray *rowIndecesInSection = [self.sections objectForKey:category];
    return [rowIndecesInSection count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *category =[self categoryForSection:section];
    return category;
}

Please help me fix this. It has had me stuck for days! Thank you! Matt

Boingoloid
  • 107
  • 2
  • 9
  • 1
    You say you believe the error is happening in your `cellForRowAtIndexPath` method. You need to be sure. Use the debugger and find out exactly which line is causing the crash and update your question accordingly. – rmaddy May 15 '15 at 18:37
  • @rmaddy I did that already, which is why I was able to say the exact line that was causing the problem. I've deleted "i believe" so now it just reads, "I've located the exact line." instead of "I believe I've located the exact line." – Boingoloid May 18 '15 at 18:22

1 Answers1

3

I think the problem lies in declaring these as pointers:

NSInteger *section = 0;
NSInteger *rowIndex = 0;

(Note the weird multiples of 8 in the numbers in your dictionaries - that's because pointer arithmetic works differently from "normal" arithmetic). Try with

NSInteger section = 0;
NSInteger rowIndex = 0;
pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • That did it. Thank you so much! And thank you for explaining why I was seeing the multiples of 8. I was so confused as to what was happening. Much appreciated. – Boingoloid May 18 '15 at 18:09