Dimly's Lab

iPhone アプリ開発のメモとか。

RSS     Archives
 

スポンサーサイト

 
上記の広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書く事で広告が消せます。
 

TableView セルの削除

 
こまごましたところは省略で。

問題となったのは、セルを削除しても動的にリストが更新されない点。

つまり、完了ボタンの押下時にまとめて更新される。

一応解決できたのでメモ。

以下、解決前のコード

/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
// Handle deletion requests
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

 if (editingStyle == UITableViewCellEditingStyleDelete) {

  [dataList removeObjectAtIndex:indexPath.row];

  [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
          withRowAnimation:UITableViewRowAnimationFade];
 }
}

// Enter edit mode by changing "Edit" to "Done"
-(void)enterEditMode {
 // Add the edit button
 self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
          initWithTitle:@"完了"
          style:UIBarButtonItemStylePlain
          target:self
          action:@selector(leaveEditMode)]
          autorelease];

 [self.tableView setEditing:YES animated:YES];
 [self.tableView beginUpdates];
}

// Finished with edit mode, so restore the buttons and commit changes
-(void)leaveEditMode {
 // Add the edit button
 self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
          initWithTitle:@"編集"
          style:UIBarButtonItemStylePlain
          target:self
          action:@selector(enterEditMode)]
          autorelease];
 
 [self.tableView endUpdates];
 [self.tableView setEditing:NO animated:YES];
}
/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/



そんでもって、以下が解決後のコード

/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
// Handle deletion requests
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

 [self.tableView beginUpdates];

 if (editingStyle == UITableViewCellEditingStyleDelete) {

  [dataList removeObjectAtIndex:indexPath.row];

  [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
          withRowAnimation:UITableViewRowAnimationFade];
 }

 [self.tableView endUpdates];
}

// Enter edit mode by changing "Edit" to "Done"
-(void)enterEditMode {
 // Add the edit button
 self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
          initWithTitle:@"完了"
          style:UIBarButtonItemStylePlain
          target:self
          action:@selector(leaveEditMode)]
          autorelease];

 [self.tableView setEditing:YES animated:YES];
}

// Finished with edit mode, so restore the buttons and commit changes
-(void)leaveEditMode {
 // Add the edit button
 self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
          initWithTitle:@"編集"
          style:UIBarButtonItemStylePlain
          target:self
          action:@selector(enterEditMode)]
          autorelease];
 
 [self.tableView setEditing:NO animated:YES];
}
/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

違いは何かと言うと

 [self.tableView beginUpdates];
 [self.tableView endUpdates];

の位置。


解決前は、編集ボタンと完了ボタンそれをれを押下した時点で呼び出していた。

だから、削除の処理を完了して、ボタンを押下するまで表示が更新されなかった。


解決後は、「削除の命令が飛んで来て、配列からデータを削除」という処理の

前後にそれぞれを記述している。

つまり、削除命令を受け取って、削除処理を完了する度に時点で表示の更新をしている。


…表現が難しい。

解決前は、全ての作業を完了してから更新している。
解決後は、1件の作業を完了する度に更新している。

と、言えばわかりやすくはなるのか?

とにかく、ひとまず解決。

Comment


    
Profile

Author : Dimly


Calendar
04 | 2012/05 | 06
S M T W T F S
- - 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 - -




Search