2010-05-21 52 views
1

我有一個UITableViewDataSource,用於兩個不同的UITableViews。在其中一個表格視圖中,我想啓用滑動刪除功能,所以我實現了tableView:commitEditingStyle:forRowAtIndexPath,並且按預期工作。但是,在另一個表中,我想禁用該功能。允許在UITableViewDataSource的一個實例中刷卡刪除,但不是另一個

我已經通過創建兩個UITableViewDataSource類,一個子類化另一個,並且我只在子類中實現了tableView:commitEditingStyle:forRowAtIndexPath。我稱它們爲RecipientModel和RecipientModelEditable。

我想知道是否有更好的方法。

回答

4

我想你的意思是這樣的:

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (self.isEditable) { 
     return UITableViewCellEditingStyleDelete; 
    } 
    return UITableViewCellEditingStyleNone; 
} 

,然後在commitEditingStyle,如果它的不可編輯

2

您可以創建兩個相同類的實例RecipientModel。設置一個BOOL實例變量,可能名稱爲isEditable。您的界面可能是這樣的:

@interface RecipientModel : NSObject <UITableViewDataSource> { 
    BOOL isEditable; 
} 

@property (readwrite) BOOL isEditable; 

@end 

以及實現可能是這樣的:

@implementation RecipientModel 

@synthesize isEditable; 

- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
    forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (self.isEditable) { 
     // Allow swipe. 
    } else { 
     // Disallow swipe. 
    } 
} 

@end 

有一點要注意的是,大多數iPhone應用程序使用UITableViewController來實現自己的表視圖的數據源和委託方法。這種方法對您的應用程序也可能更有意義。

+0

是的,但我怎麼「禁止刷卡」,在沒有做任何事情了'的tableView:commitEditingStyle:forRowAtIndexPath'?通過簡單的定義方法,用戶可以輕掃*查看*刪除按鈕。我不希望它顯示出來。 – synic 2010-05-21 17:01:24

+0

你想讓單元格可編輯嗎?如果沒有,那麼你可以通過在'-tableView:canEditRowAtIndexPath:'中返回'NO'來解決這個問題。否則,如果僅僅定義該方法就足夠了,那麼就必須求助於你的子類化方法或者開始搞亂Objective-C運行時。 – 2010-05-21 17:50:42

+0

另請參閱:http://stackoverflow.com/questions/969313/uitableview-disable-swipe-to-delete-but-still-have-delete-in-edit-mode – 2010-05-21 17:52:44

相關問題