4

UITableView に新しい行を追加するのに問題があります。私はstackoverflowで同様の質問を読み、それをグーグルで検索しました...助けにはなりませんでした。

UITableView のデータを含む空の NSMutableArray *dataForList があります。画面をクリックした後、新しい行を追加したいと思います。このエラーは次のことを示しています。

*キャッチされない例外 'NSInternalInconsistencyException' が原因でアプリを終了しています。

コード:

NSArray *insertIndexPaths = [NSArray arrayWithObject: 
                             [NSIndexPath indexPathForRow:
                              [self.dataForList count] // is zero now
                              inSection:0]];

[self.dataForList addObject:newRow];
// [self.dataForList count] is 1 now

[self.unsignedTableView beginUpdates];
[self.unsignedTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationNone];
[self.unsignedTableView endUpdates]; // on this line error ocours

私は何が欠けていますか?

すべての UITableView メソッド

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

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

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
return [self.mainController.dataForList count];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath{
return 38.0f;
}

- (UITableViewCell *) tableView: (UITableView *) tableView
      cellForRowAtIndexPath: (NSIndexPath *) indexPath {
// call render method in view, it works fain
return [self.signUpToTestView renderUnsignedCell:tableView cellForRowAtIndexPath:indexPath];
}
4

10 に答える 10

7

作成したテーブルビューにデリゲートとデータソースが設定されていることを確認してください。

例:

 [self.unsignedTableView setDelegate:self];
 [self.unsignedTableView setDatasource:self];

このクラッシュの理由は、データソースとデリゲートメソッドが呼び出されないことだと思います

于 2012-07-24T05:39:40.450 に答える
3

insertRowsAtIndexPathsmake sureを使用して行を追加すると、 UITableViewDataSourceメソッドは、追加されたセルの新しい量と新しいオブジェクトも返します。

と を確認tableView:numberOfRowsInSection:してくださいtableView:cellForRowAtIndexPath:

そうしないと、NSInternalInconsistencyException例外が発生します。

また、insertRowsAtIndexPaths を使用してすべてのセルを追加しようとする場合は、やっていることをやめて、これを読んでください。

于 2012-04-04T10:23:50.963 に答える
3

クリックした後、配列に従ってnumberOfSectionandを変更していますか?numberOfRowsInSection

return [arr count];

また、テーブルをリロードしてみてください

 [table reloadData];
于 2012-04-04T10:24:33.727 に答える
1

私はこれと同じ問題に遭遇しましたが、データをリストに追加してから、次のようにテーブルをリロードする方が簡単であることに気付きました。

    [self.dataForList addObject:newRow];
    [self.unsignedTableView reloadData];

次に、デリゲート メソッドが引き継ぎ、正しい行数を返します。

     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        return [self.unsignedTableView count];
     }
于 2012-06-19T19:55:30.040 に答える
1

問題の原因は、データ ソースとユーザー インターフェイスを 2 つの異なるポイントで変更していることにあります。データ ソースへの更新と挿入呼び出しの両方が、同じ開始/終了ブロックにある必要があります。

したがって、addObject 呼び出しの前に beginUpdates 呼び出しを移動するだけで問題を解決できます。

于 2012-10-12T12:56:20.417 に答える
0

ここに考えがあります

イベント呼び出し時 (配列の内容が変更されたとき) にブール値を true [tableView reloadData] に設定します。tableView:numberOfRowsInSection: の数値をリセットします。

それに応じて tableView:cellForRowAtIndexPath: も変更してください。

于 2012-04-04T10:28:27.680 に答える
0

同じ問題に遭遇し、問題を修正しました。テーブルの作成元である NSMutableArray を初期化していませんでした。私はすべてを正しく行っていましたが、その重要な部分を忘れていました。

myArrayName = [[[NSMutableArray alloc] init]];

コードを変更する前に、一歩下がってコードを確認することをお勧めします。数か月後ですが、これがお役に立てば幸いです。

于 2012-08-17T07:03:42.120 に答える
0
if (indexpath) {
      [self.tableView beginUpdates];
      [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexpath] withRowAnimation:UITableViewRowAnimationMiddle];
      [self.tableView endUpdates];
}
于 2012-04-04T11:27:49.107 に答える
0

古い質問への回答..

NSArray *insertIndexPaths = [NSArray arrayWithObject: 
                             [NSIndexPath indexPathForRow:
                              [self.dataForList count] // is zero now
                              inSection:0]];

[self.dataForList addObject:newRow];
// [self.dataForList count] is 1 now

それらを交換すると、動作するはずです:

[self.dataForList addObject:newRow];
// [self.dataForList count] is 1 now

NSArray *insertIndexPaths = [NSArray arrayWithObject: 
                             [NSIndexPath indexPathForRow:
                              [self.dataForList count] // is also 1 now, hooray
                              inSection:0]];
于 2012-08-20T12:52:04.383 に答える