なんて言葉があってもおかしくないほど頻繁に利用するのがテーブル。
テーブルの扱いでインターフェースの善し悪しが決まってしまうのは、androidでもiphoneでも、WEBアプリでも同じです。
というわけで、ここではiphone開発のテーブル処理を記述していきます。
まずは、UINavigationItemに編集ボタンを追加する方法
- (void)viewDidLoad {
NSLog(@"%s", __func__);
self.navigationItem.rightBarButtonItem = [self editButtonItem];
}
上記の記述をすると、ナビゲーションの右側に編集ボタンが設置されます。
設置された編集ボタンを押下すると、以下のメソッドが呼び出されます。
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
NSLog(@"%s", __func__);
[super setEditing:editing animated:animated];
}
編集ボタン押下後に、デフォルト動作以外でインターフェースを変更したい場合はここに処理を記述します。
編集ボタンを押下すると、削除ボタンが表示されます。その削除ボタンを押下すると、以下のメソッドが呼び出されます。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", __func__);
if (editingStyle == UITableViewCellEditingStyleDelete) {
// NSLog(@"before count is %d", [self.list count]);
NSInteger row = [indexPath row];
// この処理を実行すると、tableView:numberOfRowsInSectionが呼び出される
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
削除処理の場合は、引数editingStyleにUITableViewCellEditingStyleDeleteの値が格納されます。
deleteRowsAtIndexPathsメソッドはテーブルの削除処理と表示をおこないます。
また追加の場合は
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:insertIndexPath_] withRowAnimation:UITableViewRowAnimationFade];
の処理を記述します。
列の追加と削除はアニメーションを使って、ユーザーが目で確認できるようにするべきです。



