我在我的導航欄中有一個編輯按鈕,並且我有一個表格視圖。如何從表視圖中刪除單元格?
我的編輯按鈕調用一個-editAction方法。
然後,我有這段代碼來刪除一個單元格,但我不知道如何使編輯按鈕來調用這個代碼......或編輯按鈕如何讓表格視圖顯示那些紅色刪除圈子的每一個細胞,然後觸發此:
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object at the given index path
NSManagedObject *eventToDelete = [eventsArray objectAtIndex:indexPath.row];
[managedObjectContext deleteObject:eventToDelete];
// Update Event objects array and table view
[eventsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
// Commit the change
NSError *error;
if (![managedObjectContext save:&error]) {
// Handle the error
}
}
}
editButtonItem從哪裏來?在UITableView或UITableViewController中沒有這樣的屬性,在整個示例代碼中也沒有定義editButtonItem。令人困惑... – openfrog 2010-01-15 12:30:35
它來自UIViewController的類別UIViewControllerEditing,請參閱UIViewController.h。 – Costique 2010-01-15 13:03:28
Costique,這看起來像一個很好的答案,但這是特定於UITableViewController?否則,不確定UITableView如何獲取setEditing:animated:消息? OP不是關於視圖控制器類的具體內容,這就是爲什麼我給出了我的答案,但如果是這樣的話,你的答案會更可取。謝謝 – cidered 2010-01-15 13:34:56