2011-03-01 4 views
3

ユーザーが編集を押したときに、挿入コントロール(緑色プラス)を含む行をテーブルビューに追加しようとしています。これまでのところ、私が見るに挿入行を持っているが、ユーザーはそれが編集モードのときにテーブルビューをスクロールしようとすると、アプリは次のエラーでクラッシュ:私はsimilar questionことを知っているテーブルビューに行を追加すると、アプリがクラッシュするのはなぜですか?

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[_PFArray objectAtIndex:]: index (9) beyond bounds (9)'

前に尋ねられましたが、私がプログラミングに新しいので、もう少し手持ちが必要かもしれません。その質問への答えは、numberOfRowsInSectionが適切に更新されていることを確認することを提案しました。私は私のことだと思っていますが、私は明らかにどこかで間違いを犯しています。

これは、私のUINavigation Controllerのルートビューコントローラである私のテーブルビューコントローラでこれまでに得られたものです。現時点では、それは単なるダミーのテーブルです - 編集ボタン以外のものは何も接続されていません。

#import "RootViewController.h" 
#import "AppDelegate_iPhone.h" 
#import "Trip.h" 

@implementation RootViewController 
@synthesize trips = _trips; 
@synthesize context = _context; 


- (id)init 
{ 
    self = [super init] ; 
    if (self) 
    { 
     automaticEditControlsDidShow = NO; 
    } 
    return self ; 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Trip" inManagedObjectContext:_context]]; 
    NSError *error; 
    self.trips = [_context executeFetchRequest:fetchRequest error:&error]; 
    self.title = @"Trips"; 
    [fetchRequest release]; 

    // Display an Edit button for this view controller. 
    self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    int rows = [_trips count]; 
    if (tableView.editing) rows++; 
    return rows; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 
    Trip *info = [_trips objectAtIndex:indexPath.row]; 
    if (tableView.editing) 
    { 
     if (indexPath.row == 0) 
      cell.textLabel.text = @"Add New Trip"; 
     if (indexPath.row == !0) 
      cell.textLabel.text = info.tripName; 
    } 
    else 
    { 
     cell.textLabel.text = info.tripName; 
    } 
    return cell;  
} 


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    int row = indexPath.row; 

    if (self.editing && row == 0) { 
     if (automaticEditControlsDidShow) 
      return UITableViewCellEditingStyleInsert; 
     return UITableViewCellEditingStyleDelete; 
    } 
    return UITableViewCellEditingStyleDelete; 
} 


- (void)setEditing:(BOOL)editing animated:(BOOL)animated 
{ 
    automaticEditControlsDidShow = NO; 
    [super setEditing:editing animated:animated]; 

    NSArray *addRow = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0],nil]; 
    [self.tableView beginUpdates]; 
    if (editing) { 
     automaticEditControlsDidShow = YES; 
     [self.tableView insertRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft]; 
    } else { 
     [self.tableView deleteRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft]; 
    } 
    [self.tableView endUpdates]; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

- (void)viewDidUnload { 
} 

- (void)dealloc { 
    [_trips release]; 
    [_context release]; 
    [super dealloc]; 
} 


@end 

ありがとう!

+1

私は本当に興味があります、その '==!0'構文はどこから来たのですか?それは本当にあなたがしたいことをやっていませんが、なぜあなたがそれを書いたのか知​​りたいのですが... – jv42

答えて

3

部品の限界を超えているため、クラッシュがある

は、あなたのテーブルビューがeditingmodeである想像してみて間違っています。 _tripsには10個のオブジェクトがあります。

if (tableView.editing) rows++; 

そして、テーブルビューは、ここで指数10で要素にアクセスしようとします:

Trip *info = [_trips objectAtIndex:indexPath.row]; 

しかし、あなたが持っていない

は、あなたが11個の、配列内の行を持つテーブルビューを伝えますインデックスが10の要素

配列のインデックスを与えるロジックを変更する必要があります。多分このように

if (tableView.editing) 
{ // tableview row count is _trips count + 1 
    if (indexPath.row == 0) 
     cell.textLabel.text = @"Add New Trip"; 
    if (indexPath.row != 0) { 
     // realIndex = table view index - 1 
     Trip *info = [_trips objectAtIndex:indexPath.row - 1]; 
     cell.textLabel.text = info.tripName; 
    } 
} 
else 
{ 
    Trip *info = [_trips objectAtIndex:indexPath.row]; 
    cell.textLabel.text = info.tripName; 
} 

とbtw。 if (indexPath.row == !0)は何かをするif (indexPath.row != 0)

+0

ありがとう!私は私の髪を引き裂いているが、それは完全に働いた!私たちがここにいる間、 "==!0"と "!= 0"の違いを教えてもらえますか?私が理解しているように、1つは「0に等しくない」ことを意味し、もう1つは「0以外の何かに等しい」を意味します。 –

+0

@Ric 'x ==!0'は' xが(0ではない) 'を意味し、' not 0'は1を意味するので、 'x ==!0'という表現は' x == 1'と同じです。しかし、 'x!= 0'は' xが0に等しくない 'ことを意味し、0以外のすべてに対して真です。 –

+0

ありがとうございます - 私は理解していると思います!私はそれが(0ではない)が(1)と同じであるとみなされるのはちょうどCの規則であると仮定します。すべてのあなたの助けをありがとう! –

0

これは、配列に格納する行の数が問題であることを意味します。 Pls配列を確認します。アレイの要素の合計数は、あなたのtableView:cellForRowAtIndexPath:方法の配列

関連する問題