在这个应用程序中,我从Parse下载数据。该数据是5个消息的列表,每个消息都有一个类别。有4个不同的类别,因为其中两个消息具有相同的类别。我想将这些数据放在tableview的各个部分。由于数据尚未准备好进行切片,因此我必须创建2个可变数组,其作用类似于索引查找。 (遵循本指南:https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections)
问题:我收到此错误: - [__ NSArrayM objectAtIndex:]:索引8超出界限[0 .. 4]'
问题是,为什么我会收到此错误以及如何解决?
我找到了确切的问题。首先,这就是你需要的。
第一个可变词典: self.sectionToCategoryMap //此属性将部分标题映射到数据的行indeces。输出看起来像这样:(读作,从索引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;
}
请帮我解决这个问题。它让我困了好几天!谢谢! 马特
答案 0 :(得分:3)
我认为问题在于将这些声明为指针:
NSInteger *section = 0;
NSInteger *rowIndex = 0;
(注意字典中数字的奇数倍数为8 - 这是因为指针算法与“普通”算术的工作方式不同)。试试
NSInteger section = 0;
NSInteger rowIndex = 0;