insertRowsAtIndexPaths:withAnimation:抛出NSRangeException

时间:2011-05-12 09:26:31

标签: objective-c ios ios4

这是我的代码(来自核心数据教程):

[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是一个我填充表视图的数组(可能。至少在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;
}

4 个答案:

答案 0 :(得分:3)

有同样的问题。只需要告诉tableview它有1个部分也可以添加。如果不更新,则默认为0.

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

答案 1 :(得分:0)

请检查用于加载表格视图的数据源吗?它似乎是一个空数组。

答案 2 :(得分:0)

在我看来,您的eventsArray已填充并正常工作,但tableview数据源上的更新无法正常工作。这可能意味着数据源未正确设置,或者您的tableview数据源方法可能返回错误的值?

子类中以下方法背后的代码是什么? (假设您正确符合UITableViewDataSource协议)。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

答案 3 :(得分:0)

尝试将此添加到控制器中的viewdidLoad:

eventsArray = [[NSMutableArray alloc] init];
相关问题