gpt4 book ai didi

objective-c - insertRowsAtIndexPaths :withAnimation: throws NSRangeException

转载 作者:可可西里 更新时间:2023-11-01 05:48:13 24 4
gpt4 key购买 nike

这是我的代码(来自 Core Data 教程):

[eventsArray insertObject:event atIndex:0];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

第三行抛出异常:

2011-05-12 13:13:33.740 Locations[8332:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
0 CoreFoundation 0x010145a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01168313 objc_exception_throw + 44
2 CoreFoundation 0x0100a0a5 -[__NSArrayM objectAtIndex:] + 261
3 UIKit 0x0010d5b3 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 6156
4 UIKit 0x000fcd36 -[UITableView insertRowsAtIndexPaths:withRowAnimation:] + 56
5 Locations 0x00003462 -[RootViewController addEvent] + 690

我是 iPhone 开发的新手,我不明白这是什么意思。我在 tableView 的零索引处插入,所以我不知道为什么它不能使用空数组。请向我澄清这一点


stacktrace

eventsArray 是我从中填充 TableView 的数组(可能。至少它在 cellForRowAtIndexPath 中使用)


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// A date formatter for the time stamp.
static NSDateFormatter *dateFormatter = nil;
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
}

// A number formatter for the latitude and longitude.
static NSNumberFormatter *numberFormatter = nil;
if (numberFormatter == nil) {
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:3];
}

static NSString *CellIdentifier = @"Cell";

// Dequeue or create a new cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

Event *event = (Event *)[eventsArray objectAtIndex:indexPath.row];

cell.textLabel.text = [dateFormatter stringFromDate:[event creationDate]];

NSString *string = [NSString stringWithFormat:@"%@, %@",
[numberFormatter stringFromNumber:[event latitude]],
[numberFormatter stringFromNumber:[event longitude]]];
cell.detailTextLabel.text = string;

return cell;
}

最佳答案

遇到了同样的问题。只需要告诉 tableview 它也有 1 个部分要添加。如果您不更新它,它默认为 0。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

关于objective-c - insertRowsAtIndexPaths :withAnimation: throws NSRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5975932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com