1

最初のセクションには常に 3 つの行があります。一部のコンテンツがダウンロードされた後、2 番目のセクションに新しいセルを表示したいので、次のようにします。

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

しかし、私は NSInternalConsistencyException を取得します:

キャッチされない例外 'NSInternalInconsistencyException' が原因でアプリを終了しています。理由: '無効な更新: セクションの数が無効です。更新後のテーブル ビューに含まれるセクションの数 (2) は、更新前のテーブル ビューに含まれるセクションの数 (1) に、挿入または削除されたセクションの数をプラスまたはマイナスした値 (0 挿入、0削除されました)」

新しいセクションが自動的に挿入されないのはなぜですか。セクションを挿入すると、新しいセクションにない行を挿入するときに問題が発生します。

新しいセクションを自動的に追加できないのはなぜですか? 確かに InsertRowAtIndexpath: NSIndexPath にもセクションが含まれているため、必要に応じてセクションの挿入も含めます。

4

2 に答える 2

3
[self.tableView insertSections:nsIndexSetToInsert withRowAnimation:UITableViewRowAnimationAutomatic];

それに応じて numberOfSectionsInTavleView と numberOfRowsInSection を設定することを忘れないでください。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    NSInteger numberOfSections;
    if (condition1) {
        numberOfSections = 2;
    } else if (condition2) {
        numberOfSections = 3;
    } else {
        numberOfSections = 4;
    }

    return numberOfSections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section
    if(section == 1) return 3;
    else return numberOfRows;
}
于 2012-05-28T12:04:57.250 に答える
0

これを ViewDidLoad に保持する

childArray1 = [[NSMutableArray alloc] initWithObjects: @"Apple", @"Microsoft", @"HP", @"Cisco", nil];
childArray2 = [[NSMutableArray alloc] init];
parentArray = [[NSMutableArray alloc] initWithObjects:childArray1, nil];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     return [parentArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
      NSInteger rowCount ;
      if (section == 0)
      {
           rowCount = [childArray1 count];
      }
      else
      {
           rowCount = [childArray2 count];
      }
      return rowCount;
}

コンテンツがサーバーからダウンロードされたら。

 if([childArray2 count] > 0)
     [parentArray addObject : childArray2];
 [tableView reloadData];

これはうまくいくはずです。

于 2012-06-02T05:59:05.577 に答える