「EditButton」はUIBarButtonItemで生成しなくても、以下のコードですぐに追加できる。
self.navigationItem.leftBarButtonItem=self.editButtonItem;
追加されたボタンをタップすると、UITableViewControllerのサブクラスでは、以下のデリゲートメソッドが呼び出され、テーブルビューのセルアイテムが削除できる。デフォルトではコメントアウトされている。
その際、削除対象のデータソース(主に配列)を追記するしなければならない(太字部分)。ちなみに「members」はNSMutableArrayクラスのインスタンス。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[appDelegate.members removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
コメントする