将部分添加到表视图,索引越界错误

Adding sections to tableview, index out of bounds error

在这个应用程序中,我从 Parse 下载数据。该数据是 5 条消息的列表,每条消息都有一个类别。有 4 个不同的类别,因为其中两个消息具有相同的类别。我想将该数据放入表视图的各个部分。由于数据尚未准备好进行分段,我不得不创建 2 个可变数组,它们的作用类似于索引查找。 (遵循本指南:https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections

问题:我收到此错误: -[__NSArrayM objectAtIndex:]: 索引 8 超出范围 [0 .. 4]'

问题是,为什么会出现此错误,我该如何解决?

我找到了问题所在的确切行。首先,这是你需要的。

第一个可变字典: self.sectionToCategoryMap //此 属性 将 部分标题 映射到数据的行索引。输出看起来像这样:(读作,从索引 0 处的 object 获取第一个类别 header)

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

第二个可变字典: self.sections // 这映射了什么项目在什么部分(类别)中。输出如下所示:

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

这两个词典是通过以下代码创建的:

- (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
    }
}

我在下面的 cellForRowAtIndexPath 中发生错误,特别是以下行: NSNumber *rowIndex = [rowIndecesInSection objectAtIndex:indexPath.row]; (注:categoryForSection是我定义的辅助方法,它的实现也在下面。)

- (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;
}

为了更好的衡量,这是我的其余代码。

- (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;
}

请帮我解决这个问题。它让我坚持了好几天!谢谢! 马特

我认为问题在于将它们声明为指针:

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

(请注意字典中数字中奇怪的 8 倍数 - 这是因为指针算法与 "normal" 算法的工作方式不同)。试试

NSInteger section = 0;
NSInteger rowIndex = 0;