开发者

Allow swipe-to-delete in one instance of UITableViewDataSource, but not another

I've got a UITableViewDataSource that I'm using for two different UITableViews. In one of the table views, I want to enable swipe-to-delete, so I've implemented tableView:commitEditingStyle:forRowAtIndexPa开发者_开发问答th, and it works as expected. However, in another table, I want to disable that feature.

I've got it working by making two UITableViewDataSource classes, one subclassing the other, and I only implement tableView:commitEditingStyle:forRowAtIndexPath in the subclass. I call them RecipientModel and RecipientModelEditable.

I'm wondering if there's a better way.


I think you mean something like this:

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

and then in the commitEditingStyle, don't do anything if its not editable


You could make two instances of the same class RecipientModel. Set a BOOL instance variable, perhaps named isEditable. Your interface might look like this:

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

@property ( readwrite ) BOOL isEditable;

@end

And your implementation might look like this:

@implementation RecipientModel

@synthesize isEditable;

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

@end

One thing to note is that most iPhone apps use a UITableViewController to implement their table view's data source and delegate methods. That approach may also make more sense for your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜