2

次のコードで削除しようとしています。

[super deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

複数の例外が返されます。

 *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:1070
2013-01-29 16:28:22.628 

キャッチされない例外 'NSInternalInconsistencyException' が原因でアプリを終了しています。理由: '無効な更新: セクション 1 の行数が無効です。更新後の既存のセクションに含まれる行数 (5) は、それに含まれる行数と等しくなければなりません更新前のセクション (5)、そのセクションから挿入または削除された行数 (0 挿入、5 削除) のプラスまたはマイナス、およびそのセクションに移動されたまたはセクションから移動された行の数 (0 移動、0 移動)アウト)。'

4

1 に答える 1

2

これは、行数を動的に返す方法が必要だからです。

たとえば、3 つの配列を作成します。それぞれに 3 つの値があります (これらはNSArray変数です):

ファイル.h内:

NSArray *firstArray;
NSArray *secondArray;
NSArray *thirdArray;

ファイルでは.m、viewDidLoad または init または同様のもの:

firstArray = [NSArray arrayWithObjects:@"Cat", @"Mouse", @"Dog", nil];
secondArray = [NSArray arrayWithObjects:@"Plane", @"Car", @"Truck", nil];
thirdArray = [NSArray arrayWithObjects:@"Bread", @"Peanuts", @"Ham", nil];

テーブルの行数を返すとき、私は持っています:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
         return array.count;
      if (section == 0) {
          return firstArray.count;
      } else if (section == 1) {
          return secondArray.count;
      } else {
          return thirdArray.count;
      }
}

次に、でcellForRow

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    if (indexPath.section == 0) {
    cell.textLabel.text = [firstArray objectAtIndex:indexPath.row];
    } else if (indexPath.section == 1) {
        cell.textLabel.text = [secondArray objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [thirdArray objectAtIndex:indexPath.row];
    }

    return cell;
}

次に@"Dog"、テーブルをスワイプするか、他の方法で削除します。次に、テーブルをリロードすると、配列カウントが 2 になるため、テーブルは 2 行のみを表示する必要があることを「認識」します。基本的に、データ ソースも更新する必要があります。他のセクションにも適用されます。配列から要素を削除するため、行数も更新されます。

于 2013-01-29T11:47:33.220 に答える