1

TableViewセル間にある程度の間隔を空けたいので、TableViewコードを次のように変更しました。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [_boxs count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 1;
}

私の問題は、「行の追加\オブジェクト」コードがクラッシュすることです。このコードにどのような変更を加える必要がありますか?

問題のあるコード:

SomeClass *newObject = [[SomeClass alloc]initWithTitle:@".....  
[_boxs addObject:newObject];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_boxs.count-1 inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];    
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:NO];    
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self performSegueWithIdentifier:@"FirstDetailsSegue" sender:self ];

-boxsNSMutableArrayです。

これは私が得ているエラーです:*キャッチされない例外のためにアプリを終了します'NSInternalInconsistencyException'、理由:'セクション0に行7を挿入しようとしましたが、更新後にセクション0に1行しかありません'

4

1 に答える 1

2

作成している行の行と列を切り替えましたNSIndexPath。コードを次のように変更する必要があります。

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:_boxs.count-1];

1つのセクションと複数の行のみを使用することを計画している場合は、デリゲートメソッドを変更して1つのセクションと[_boxs count]行のみを返すようにする必要があります。その場合、コードは機能します。

コメントによると、行ではなく新しいセクションを挿入したいようです。これを試して、

[self.tableView insertSections:[NSIndexSet indexSetWithIndex:_boxs.count-1] withRowAnimation:UITableViewRowAnimationAutomatic];

コードからinsertRow行を削除できます。

于 2012-12-19T20:34:04.177 に答える