【发布时间】:2015-05-15 18:17:39
【问题描述】:
在这个应用程序中,我从 Parse 下载数据。该数据是 5 条消息的列表,每条消息都有一个类别。有 4 个不同的类别,因为其中两个消息具有相同的类别。我想将这些数据放入 tableview 的部分中。由于数据尚未准备好进行分段,因此我必须创建 2 个可变数组,它们的作用类似于索引查找。 (按照本指南:https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections)
问题:我收到此错误: -[__NSArrayM objectAtIndex:]: 索引 8 越界 [0 .. 4]'
问题是,为什么我会收到此错误,我该如何解决?
我找到了问题所在的确切线路。首先,这是您需要的。
第一个可变字典: self.sectionToCategoryMap //此属性将章节标题映射到数据的行索引。输出看起来像这样:(读作,从索引 0 处的对象获取第一个类别标题)
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;
}
请帮我解决这个问题。它让我卡了好几天!谢谢! 马特
【问题讨论】:
-
您说您认为错误发生在您的
cellForRowAtIndexPath方法中。你需要确定。使用调试器并准确找出导致崩溃的行并相应地更新您的问题。 -
@rmaddy 我已经这样做了,这就是为什么我能够说出导致问题的确切行。我已经删除了“我相信”,所以现在只写着“我已经找到了确切的路线”。而不是“我相信我已经找到了确切的路线。”
标签: ios objective-c xcode uitableview sections