0

いくつかの条件に応じて、さまざまな数のセクションを持つ UIView に UITableView があります。以下の例外を受け取っています。

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

エラーから、データソースに異なる数のセクションがあるようです??? 問題を解決するにはどうすればよいですか?

これは私のインターフェースです:

@interface bookDetailView :  SomeView <UITableViewDelegate,UITableViewDataSource> 
{
    UITableView *bookDetailTableView;
        ....
}

これが私のビューの初期化です

UIView *view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.view = view;

UITableView *tableView = [[[UITableView alloc] initWithFrame:view.bounds style:UITableViewStyleGrouped] autorelease];
tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.delegate = self;
tableView.dataSource = self;
tableView.sectionIndexMinimumDisplayRowCount = 20;

bookDetailTableView = tableView;
[self.view addSubview:bookDetailTableView];

コードは insertSections ... でクラッシュします。

if (![book method1])
{
    [bookDetailTableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1,1)] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
     [bookDetailTableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2,1)] withRowAnimation:UITableViewRowAnimationFade];
}

numberOfSectionsInTableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
{
    if (editMode)
    {


        if(newbookFlag || listBook )
        {
            return 1;
        }
        else 
        {
            if(![book preferable])
            {
                return 2;
            }
            else
            {
                return 3;
            }
        }
    }


}

何か案が?beginUpdates と endUpdates を試しましたが、まだ問題が解決していません。

4

2 に答える 2

0

セクションを UITableView に挿入する場合、この変更が dataSource に反映されていることを確認する必要があります。この場合、numberOfSectionsInTableView: メソッドは、新しいセクションの挿入時に追加の 1 行を返す必要があります。

于 2012-10-03T10:46:00.427 に答える