2011-10-08 99 views
2

我已經實現了一個自定義編輯配件視圖,如我在回答this問題中所述。大多數情況下,它工作得很好,但我注意到它的一個小問題。UITableViewCell自定義editingAccessoryView - 沒有正確解僱

當我在表格視圖中滾動或選擇另一行時,我的自定義編輯附件不會被解除。使用標準編輯附件(刪除按鈕),可以捕捉桌面上任意位置的觸摸並用於刪除刪除附件視圖 - 例如,您可以在內置的Notes應用程序中或在任何其他地方一個標準的編輯配件視圖。

這一定是因爲我在刷卡模式下返回UITableViewEditingStyleNone。但是,如果我返回任何其他模式,那麼我的自定義編輯配件不會顯示。

如何獲取標準編輯樣式的功能,其中桌面視圖上的任意位置的觸摸都會取消編輯附件?

單元格不是子類,但是它是使用自定義佈局從nib文件加載的。編輯配件視圖是nib文件的一部分,並通過editingAccessoryView插座連接。

我已成功地實現中途通過存儲滑動到編輯行的索引路徑並且如果選擇另一行或滾動開始於表設定該單元退出編輯模式我想要的效果。不過,我想正確地做。

+0

@jrturton我得到同樣的問題..我已經參考http://stackoverflow.com/questions/7295834/custom-editingaccessoryview-not-working?lq=1。你能告訴我你在代碼中做了什麼改變的解決方案,謝謝! –

回答

2

我能夠解決這個問題,但遺憾的是,它需要額外的工作,並不像設置一些屬性一樣簡單。

在我

- (UITableViewCellEditingStyle)tableView:(UITableView *)_tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 

方法我返回UITableViewCellEditingStyleNone讓我的自定義editingAccessoryView會顯示出來。在這種方法我也這樣做:

self.tableView.scrollEnabled = NO; 
if(self.editingPath) 
{ 
    [[tableView cellForRowAtIndexPath:editingPath] setEditing:NO animated:YES]; 
} 

self.editingPath = indexPath;  
for (UITableViewCell *cell in [tableView visibleCells]) 
{ 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 

這將禁用滾動,然後存儲我們刷卡,供以後使用indexPath。如果你在另一行上滑動,在編輯一行時,它會將第一行編輯並編輯第二行,這就是蘋果應用程序的行爲方式。我還將所有可見單元上的單元格selectionStyle設置爲UITableViewCellSelectionStyleNone。這可以減少用戶在當前正在編輯的時候選擇另一個單元格時的藍色閃爍。

接下來我們需要在點擊另一個單元時關閉accessoryView。要做到這一點,我們實現此方法:

-(NSIndexPath *)tableView:(UITableView *)_tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(self.editingPath) 
{ 
    UITableViewCell *c = [tableView cellForRowAtIndexPath:self.editingPath]; 
    [c setEditing:NO animated:YES]; 

    self.tableView.scrollEnabled = YES; 
    self.editingPath = nil; 
    for (UITableViewCell *cell in [tableView visibleCells]) 
    { 
     cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
    } 

    return nil; 
} 

return indexPath; 
} 

這樣做是什麼時候有人要點擊一個細胞,如果我們說了unedit細胞,然後編輯並返回什麼。

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

我回到YES,使上我希望用戶能夠刪除單元格編輯。