Iphone 允许在UITableViewDataSource的一个实例中删除滑动,但不允许在另一个实例中删除

Iphone 允许在UITableViewDataSource的一个实例中删除滑动,但不允许在另一个实例中删除,iphone,Iphone,我有一个UITableViewDataSource,用于两个不同的UITableView。在其中一个表视图中,我想启用滑动删除功能,因此我实现了tableView:committeditingstyle:forRowAtIndexPath,并按预期工作。但是,在另一个表中,我想禁用该功能 我创建了两个UITableViewDataSource类,一个是另一个的子类,我只在子类中实现了tableView:CommittedItingStyle:ForRowatingIndexPath。我称之为Re

我有一个UITableViewDataSource,用于两个不同的UITableView。在其中一个表视图中,我想启用滑动删除功能,因此我实现了
tableView:committeditingstyle:forRowAtIndexPath
,并按预期工作。但是,在另一个表中,我想禁用该功能

我创建了两个UITableViewDataSource类,一个是另一个的子类,我只在子类中实现了
tableView:CommittedItingStyle:ForRowatingIndexPath
。我称之为RecipientModel和RecipientModelEditable


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

您可以创建同一类的两个实例
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
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isEditable) {
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleNone;
}

需要注意的一点是,大多数iPhone应用程序都使用
UITableViewController
来实现其表视图的数据源和委托方法。这种方法可能对您的应用程序更有意义。

您可以创建同一类的两个实例
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
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isEditable) {
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleNone;
}

需要注意的一点是,大多数iPhone应用程序都使用
UITableViewController
来实现其表视图的数据源和委托方法。这种方法可能对您的应用程序更有意义。

我认为您的意思是:

@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
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isEditable) {
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleNone;
}

然后在
committeeditingstyle
中,如果它不可编辑,请不要执行任何操作

我想你的意思是这样的:

@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
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isEditable) {
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleNone;
}

然后在
committedingstyle
中,如果其不可编辑,则不执行任何操作

是,但是如何在
表视图中“禁止刷卡”:committedingstyle:forRowAtIndexPath
?通过简单地定义方法,用户可以滑动查看删除按钮。我根本不想让它显示。你想让单元格可以编辑吗?如果没有,则可以通过在
-tableView:canEditRowAtIndexPath:
中返回
NO
来解决此问题。否则,如果仅仅定义方法就足够了,那么您将不得不求助于子类化方法,或者开始与Objective-C运行时混为一谈。另外,请参见:是,但是如何在
表视图:CommittedItingStyle:ForRowatineXpath
中“禁止刷卡”?通过简单地定义方法,用户可以滑动查看删除按钮。我根本不想让它显示。你想让单元格可以编辑吗?如果没有,则可以通过在
-tableView:canEditRowAtIndexPath:
中返回
NO
来解决此问题。否则,如果仅定义方法就足够了,您将不得不求助于子类化方法或开始与Objective-C运行时混为一谈。另外,请参见以下内容: