2012-04-22 7 views
4

NSUserDefaultsオブジェクトからコンテンツがロードされたUITableViewの作成に取り組んでいます。私は、プログラムがクラッシュした、しかし、セル/セクションを削除しようと次を吐き出すたび:セルとセクションを削除しようとするとUITableViewがクラッシュする

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1030 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). 

どんなに私はこのエラーをよく読んで、それを修正しようとどのくらい、私はここで立ち往生していないしてきました過去数時間

(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //[[[NSUserDefaults standardUserDefaults] objectForKey:@"rawArray"] removeObjectAtIndex:indexPath.row]; 
     //[[[NSUserDefaults standardUserDefaults] objectForKey:@"rawDates"] removeObjectAtIndex:indexPath.row]; 

     NSMutableArray *rawArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"rawArray"]; 
     NSMutableArray *rawDates = [[NSUserDefaults standardUserDefaults] objectForKey:@"rawDates"]; 

     [rawArray removeObjectAtIndex:indexPath.row]; 
     [rawDates removeObjectAtIndex:indexPath.row]; 

     [[NSUserDefaults standardUserDefaults] setObject:rawArray forKey:@"rawArray"]; 
     [[NSUserDefaults standardUserDefaults] setObject:rawDates forKey:@"rawDates"]; 

     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; 
     [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight]; 

    }  
} 

、ここでは、行とセルの数を決定するために使用される方法です:ここでは、ユーザーがボタンを「削除」をタップしたときに呼び出され、私の方法である

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return [[[NSUserDefaults standardUserDefaults] arrayForKey:@"rawDates"] count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 

    return [[[NSUserDefaults standardUserDefaults] arrayForKey:@"rawDates"] objectAtIndex:section]; 
} 

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

そこの最後の方法今の時点で、私は各セクションごとに1つのセルしか持っていないので、1つを返します。ここを見ていただきありがとうございます。本当にありがとうございます。 :/

+1

次のようになり

@interface UITableView (PSAdditions) - (void)ps_updates:(void (^)(void))updateBlock; @end @implementation UITableView (PSAdditions) - (void)ps_updates:(void (^)(void))updateBlock; { [self beginUpdates]; if (updateBlock) { updateBlock(); } [self endUpdates]; } 

UITableViewの[のtableView beginUpdates];'と '[のtableView endUpdates];' –

+0

はあなたですそれに応じてデータソースモデルを更新する。 –

答えて

14

は、基本的には、これらの方法のいずれかの呼び出し

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; 
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; 
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; 

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation; 
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation; 
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; 

例えば

- (void)beginUpdates; 
- (void)endUpdates; 

間を置く必要がある Batch Insertion, Deletion, and Reloading of Rows and Sections

をチェック

[tableView beginUpdates]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; 
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight]; 
[tableView endUpdates]; 

あなたのデータソースは、時間endUpdatesによる変更を反映する必要が


個人的に

と呼ばれる私は

  1. にこの周りのブロックベースのラッパーを与えるために、カテゴリを作るのが好き電話を忘れるのをやめてくださいendUpdates
  2. 字下げを正しく見えるようにする/自動にする

カテゴリー、私はあなたが `呼び出すために忘れてしまったと思い、この

[tableView ps_updates:^{ 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; 
    [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationRight]; 
}]; 
+0

ああ、それはすぐにトリックをやった。私はどのようにシンプルなのか信じることができません - 私は本当に潜水前にすべての開発者のドキュメントを読む必要があります。私は間違いなく、提案のおかげで、UITableView(PSAdditions)サブクラスを作るに見ていきます。本当にありがとう、私の日をどれくらい作りましたか分かりません。 :) – insanj

+0

beginUpdatesとendUpdatesを使用して完全に動作していますが、初めて何かを削除します。 2回目に、私はいつもメッセージ「***キャッチされていない例外によるアプリケーションの終了 'NSInternalInconsistencyException'、理由: ' - [__ NSCFArray removeObjectAtIndex:]:変更可能なメソッドが不変オブジェクトに送信されました。私は周りを見回し、問題は私のnumberOfRowsInSectionメソッドでもっとも発生する可能性が高いことを発見しましたが、セクションごとに1つのセルしか必要としないので、私はどのように変更しますか、私は削除するものを追跡しません私はどうしたらいいか分からない)。:/ – insanj

+0

私が示したサブクラスではないクイック解明、それは 'カテゴリ'です。もしあなたが簡単なグーグルを与えるなら、2番目のコメントのためにたくさんのサンプルを見つけるでしょう - あなたはtableViewのデータソースがtableViewに加えられたすべての変更。もしあなたが助けを必要とするならば、もっと具体的な詳細をつけて新しい質問を始めるのが一番良いでしょう。 –

関連する問題