2012-01-14 9 views
0

モーダルビューコントローラを使用してUItableViewにユーザー定義のテキストを追加しています。 テーブルはNSMutableArrayとして作成されます。モーダル・ビューでデータを入力し、完了ボタンをタップすると無効な更新が行われました。無効な更新:無効なセクション数。エラーが生成されます。 ユーザーがデータを入力したときに配列を更新する必要がありますが、その方法はわかりません。関連するコードを添付しました。アドバイスありがとうございます。あなたは既に配列にこれを追加したい場合を除き<NSInternalInconsistencyException>無効な更新:無効なセクション数

    int newRowIndex = [listOfConjProcedures indexOfObject:item]; 

これは、インデックスを持っていないだろうが、次の行に、あなたはそれを追加します。

//Number of sections 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } 
//Number of rows 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [listOfConjProcedures count]; 
} 

//Loading the cells 
- (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]; 

    } 

    // Configure the cell. 

    cell.textLabel.text = [listOfConjProcedures objectAtIndex:indexPath.row]; 

    return cell; 
} 

//Done button Delegate for the modal view 
- (void)itemDetailViewController:(ItemDetailViewController *)controller didFinishAddingItem:(ChecklistItem *)item 
{ 
    int newRowIndex = [listOfConjProcedures indexOfObject:item]; 
    [listOfConjProcedures addObject:item]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0]; 
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath]; 
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 

} 
+1

numberOfSectionsメソッドの外観はどうですか? – jrturton

+0

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

答えて

1

私はあなたの問題はここかもしれないと思います配列なので、意味がありません。

インデックスパス0、NSNotFoundに行を追加すると、テーブルビューが混乱すると思います。配列のカウントを行インデックスとして使用するか、テーブルの先頭(0,0)(および配列のインデックス0)に新しい行を挿入します。

+0

ご返信ありがとうございます。項目はコードの別のセクションで定義され、UItextfieldのテキストをモーダルビューで格納することになっています。 ' - (IBAction)done { ChecklistItem * item = [[ChecklistItem alloc] init]; item.text = self.textField.text; item.checked = NO; [self.delegate itemDetailViewController:self didFinishAddingItem:item]; } ' – MacUser

+0

はい、まだlistOfProcedures配列にない場合は、インデックスとしてNSNotFoundを取得し、無効なインデックスパスを構築するために使用します。 – jrturton

+0

これは私がモーダルビューコントローラでUIlabelを設定したものであるため、item.textに変更していただきありがとうございます – MacUser