iPhone - numberOfRowsInSection 数组计数错误
如果 @"Comments" 数组为 0 并且其他两个数组为 1 或更大,我将收到以下错误。
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
这是填充 self.items 数组。
我如何解释一些 self.notification
(在本例中为注释)数组可能为空并且仍然让我的代码运行而不会抛出此类错误的事实?
NSDictionary *commentsDict = [NSDictionary dictionaryWithObject:self.notification.Comments forKey:@"Comments"];
NSDictionary *likesDict = [NSDictionary dictionaryWithObject:self.notification.Likes forKey:@"Likes"];
NSDictionary *friendsDict = [NSDictionary dictionaryWithObject:self.notification.Likes forKey:@"Friends"];
self.items = [NSMutableArray array];
[self.items addObject:commentsDict];
[self.items addObject:likesDict];
[self.items addObject:friendsDict];
这是失败的代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary *dictionary = [self.items objectAtIndex:section];
NSArray *array = nil;
if (section == 0) {
array = [dictionary objectForKey:@"Comments"]; // 0 count
} else if (section == 1) {
array = [dictionary objectForKey:@"Likes"]; // 1 count or greater
} else {
array = [dictionary objectForKey:@"Friends"]; // 1 count or greater
}
return [array count];
}
I am getting the following error if the @"Comments" array is 0 and the other two are 1 or greater.
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
This is populating the self.items
array.
How can I account for the fact that some of the self.notification
(Comments in this case) arrays may be null and still have my code run without throwing this type of error?
NSDictionary *commentsDict = [NSDictionary dictionaryWithObject:self.notification.Comments forKey:@"Comments"];
NSDictionary *likesDict = [NSDictionary dictionaryWithObject:self.notification.Likes forKey:@"Likes"];
NSDictionary *friendsDict = [NSDictionary dictionaryWithObject:self.notification.Likes forKey:@"Friends"];
self.items = [NSMutableArray array];
[self.items addObject:commentsDict];
[self.items addObject:likesDict];
[self.items addObject:friendsDict];
This is the code where it fails:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary *dictionary = [self.items objectAtIndex:section];
NSArray *array = nil;
if (section == 0) {
array = [dictionary objectForKey:@"Comments"]; // 0 count
} else if (section == 1) {
array = [dictionary objectForKey:@"Likes"]; // 1 count or greater
} else {
array = [dictionary objectForKey:@"Friends"]; // 1 count or greater
}
return [array count];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在代码前面放置一个
NSLog(@"Array: %@", self.items);
,您将看到该数组确实是空的。put a
NSLog(@"Array: %@", self.items);
in front of your code and you will see that the array is indeed empty.您试图通过不存在的索引获取对象。也许 self.items 数组没有每个部分索引的对象。从错误消息来看,
self.items
实际上是空的(零个对象)。You're trying to get an object by an index that is not there. Perhaps the
self.items
array doesn't have an object for each section index. Fromt the error message it looks likeself.items
is actually empty (zero objects).也许您定义了太多节(超过 self.items 中的项目数):
那么这将失败:
Maybe you have too many sections defined (more than the number of items in self.items):
Then this would fail:
要解决这个问题,您需要查看如何设置
self.items
以及如何实现- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
。self.items
提前释放数组,或者返回节数的方式与self.items
无关。To work this out, you'll need to look at how you set
self.items
and how you implemented- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
.Either
self.items
is releasing the array early or how you're returning the number of sections is independent ofself.items
.这是我的
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
的问题,因为在更改节标题时我仅引用 Comments 数组而不是其他两个数组。This was an issue with my
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
as I was only referencing the Comments array instead of the other two when changing the section header.