2012-02-14 26 views
0

我有一個tableviewController。在UINavigationBar我將添加一個名爲Edit的按鈕。當用戶點擊它時,我需要所有單元格進入編輯模式,在那裏他們可以刪除記錄。從表中刪除單元格 - 提供的代碼

我想下面的方法做到了。我對麼 ?當我點擊編輯按鈕時,我會得到紅色圓圈和單元格上的刪除按鈕嗎?

2.)當用戶點擊它來調用以下方法時,我如何編寫編輯按鈕(UIBarbuttonitem)的代碼?

3.)當我們刪除單元格時,我需要重新加載表中的數據。我爲此編寫了代碼,是否正確。 (我目前不在我的Mac上)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

     [self.tableView beginUpdates]; 
     [discardedItems addObject:[self.entries objectAtIndex:indexPath.row]]; 
     [self.itemsMutableArray removeObjectsInArray:discardedItems ]; 
     self.entries = [NSArray arrayWithArray:self.itemsMutableArray]; 
     [self.tableView endUpdates]; 

     [self.tableView reloadData]; 
    } 
    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 
    } 
} 
+0

你用什麼itemsMutableArray的?你不能只是將條目作爲一個可變數組嗎? – SEG 2012-02-14 16:13:04

+0

我將'entries'數組的內容複製到它,所以我可以輕鬆地刪除項目,然後將其重新分配給'entries'數組。 – shajem 2012-02-14 16:21:56

回答

1

首先,您需要指定是否可以編輯行。這是通過以下方法完成的:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 

如果希望所有行都是可編輯的,則返回yes。

要獲得紅色圓圈使用,

在由編輯按鈕調用的方法

也許......

UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit ", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(pressedEdit:)]; 
self.navigationItem.leftBarButtonItem = editButton; 

(在viewDidLoad中)

和pressedEdit:

- (void) pressedEdit :(id)sender { 
    UIBarButtonItem *editButton = (UIBarButtonItem*)self.navigationItem.leftBarButtonItem; 

    if (!self.tabeView.editing) { 
     [self.tableView setEditing:YES animated:YES]; 
     editButton.title = NSLocalizedString(@"Done", @""); 
    } 
    else { 
     [self.tableView setEditing:NO animated:YES]; 
     editButton.title = NSLocalizedString(@"Edit", @""); 
    } 
} 

根據你寫的代碼,我認爲你應該先更新數據庫然後刪除單元格...

+0

我應該在哪裏寫'[self.tableView setEditing:YES animated:YES];'?它在'viewdidLoad'方法中嗎?我應該包括'[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; '內部'[self.tableView beginUpdates]; '?? – shajem 2012-02-14 15:42:23

+0

deleteRowsAtIndexPaths應從數據數組中刪除後。 setEditing:當你想要紅圈出現時應該調用。 我也不認爲beginUpdates和endUpdates是必要的 – SEG 2012-02-14 15:53:14

+0

當創建UIBarbuttonItem我應該添加到'Action'屬性? (它應該執行哪個方法)? – shajem 2012-02-14 16:03:35

1

您的代碼似乎沒有錯。但是這麼多工作很少。你也可以用reloadData殺死動畫。你可以在beginUpdates/endUpdates中打包甚至不接觸tableView的調用。

爲什麼不首先使用NSMutableArray作爲數據源?你可以減少七行代碼翻兩番:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // delete the item from the datasource 
     [self.entries removeObjectAtIndex:indexPath.row]; 
     // Delete the row from the table view 
     [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 
    } 
} 
+0

也許他想跟蹤discardedItems數組中的已刪除項目。但我完全同意你的看法...... – SEG 2012-02-14 15:56:23

+0

是的,我需要跟蹤已刪除的記錄。但我仍然想知道代碼如何知道我是否點擊了'edit' uibarbutton來添加執行上述方法? – shajem 2012-02-14 16:02:33