iPhone - numberOfRowsInSection 数组计数错误

发布于 2024-10-19 15:24:03 字数 1387 浏览 1 评论 0原文

如果 @"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

西瓜 2024-10-26 15:24:03

在代码前面放置一个 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.

小兔几 2024-10-26 15:24:03

您试图通过不存在的索引获取对象。也许 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 like self.items is actually empty (zero objects).

魂ガ小子 2024-10-26 15:24:03

也许您定义了太多节(超过 self.items 中的项目数):

那么这将失败:

[self.items objectAtIndex:section]

Maybe you have too many sections defined (more than the number of items in self.items):

Then this would fail:

[self.items objectAtIndex:section]
够钟 2024-10-26 15:24:03

要解决这个问题,您需要查看如何设置 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 of self.items.

¢好甜 2024-10-26 15:24:03

这是我的 - (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文