2014-01-15 17 views
0

我需要在用戶點擊刪除按鈕後離開刪除模式。我希望顯示一些活動指示符,並在實際刪除單元格之前等待服務器對刪除操作的響應(或者如果服務器沒有響應,則不會)。我想從委託方法執行此操作:如何在用戶使用滑動手勢編輯時離開UITableViewCellEditingStyleDelete模式

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

我該怎麼做?

+0

check in didFinishLoading and reload the table ... –

回答

4

要隱藏「刪除」 - 鍵,您需要使用setEditing:animated:方法。

但是,您執行tableView:commitEditingStyle:forRowAtIndexPath:需要稍微延遲才能執行此操作。在參考Table View Programming Guide for iOS圖7-1下面的說明指出:

注:數據源不應該調用setEditing:動畫:從實施的tableView的範圍內:commitEditingStyle:forRowAtIndexPath :.如果由於某種原因,它必須在延遲之後通過使用performSelector:withObject:afterDelay:方法來調用它。

所以,你的實現可能是這樣的:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     // remove delete button only after short delay 
     [self performSelector:@selector(hideDeleteButton:) withObject:nil afterDelay:0.1]; 
    } 
} 

- (void)hideDeleteButton:(id)obj 
{ 
    [self.tableView setEditing:NO animated:YES]; 
} 

上面的代碼使得當用戶按下它0.1秒後刪除鍵式滑走。當然,您將需要在等待完成操作時阻止它回到編輯模式。爲此,您可以重寫UITableViewDataSource方法tableView:canEditRowAtIndexPath:以防止在等待時再次編輯同一單元格或整個表格。

+0

這是偉大的工作,不重新加載表,所以我選擇一個。我實際上已經嘗試過'performSelector:'setEditing:'方法,但它不起作用,現在不是爲什麼。 – Ossir

1

在我的代碼我作出

[self.tableView reloadData];

一個電話,我不知道這是否是做了100%的正確方式,但它爲我工作。所以你的功能可能是這樣的:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Do your processing. 

[self.tableView reloadData]; 

} 

它應該清除掉紅色的刪除按鈕並刷新您的數據。

您可能還可以玩弄呼叫

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

但在我的經驗,其reloadData呼籲,似乎這樣的伎倆

+0

它的工作原理,但我想檢查第二個答案,然後我將其標記爲正確的 – Ossir

+0

以及您選擇了哪個 – Jeef

+0

重新加載整個表格視圖對我來說不是最佳方案。而且我沒有弄清楚你的意思,因爲我不想真的刪除單元格,我希望它保持並顯示活動指標。 – Ossir

0

嗨,讓我直說:你的數據源實際上是在線的,你需要確認它在你更新你的tableview之前被刪除 - 同時你想顯示一個AI活動指示器。

如果這是你想要做什麼,然後你用這種方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// Get the key of the data you want to delete. 

data_struct_instance * data = [self.mutableArray_data_source objectForRowAtIndexPath:indexPath]; 
NSString *data_key = data.key; 
row_index = indexPath.row; // set a property to preserve the row (or index path for multilevel data) for use when you delete the the record later. 

[self start_UIActivityIndicator]; 
[self callonline_with_delete_command:data_key]; // send the request to delete the record 

    } 

啓動一次服務器取決於它是否是成功的響應,或者不是你可以刪除記錄或重新加載,如果整個陣列表小是爲了確保數據同步 -

.... 
    [activityindicator StopAnimating]; 

    if (success) { 

    [self.mutableArray_data_source removeObjectAtIndex:row_index];} 
    else { 
     NSLog .... 
     } 

    [self.tableView reloadData]; // resets the delete button. 
+0

是的,你說得對。但我想立即刪除刪除按鈕並在此單元格頂部顯示活動指示器以顯示正在進行的操作並允許用戶繼續瀏覽列表 – Ossir