2011-05-12 37 views
4

這是我的代碼(從核心數據教程):insertRowsAtIndexPaths:withAnimation:拋出NSRangeException

[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; 
} 

回答

3

有同樣的問題。只需要告訴tableview它還有1個部分要添加。如果不更新它,它默認爲0。

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

好,我認爲蘋果公司的iOS CoreData教程缺少這個,它開始變得令人沮喪。 – cfeduke 2011-06-27 19:13:24

0

能否請您檢查用於加載表視圖的數據源?它看起來是一個空陣列。

+0

好,它應該是空的,因爲現在這是第一次應用程序啓動 – 2011-05-12 09:42:09

+0

然後,你必須先加載數據源,然後加載表視圖。 – PgmFreek 2011-05-12 09:44:58

+0

我不確定你的意思。我添加了一個截圖到帖子 – 2011-05-12 09:57:26

0

在我看來,你的eventsArray被填充,並工作,但在tableview數據源的更新無法正常工作。這可能意味着數據源沒有正確設置,或者您的tableview數據源方法可能返回錯誤的值?

你的子類中以下方法背後的代碼是什麼? (假設你正確符合UITableViewDataSource協議)。

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

我發現我的錯誤。它看起來像numberOfSectionsInTableView出於某種原因返回0。我不知道爲什麼有返回0,也許我意外地做到了 – 2011-05-12 12:44:40

0

嘗試在你的控制器中添加這個到viewDidLoad中:

eventsArray = [[NSMutableArray alloc] init];