Search code examples
iosobjective-cuitableviewdelegation

iOS - Is UITableViewCellDelegate Necessary When I Have Helper Class?


I've been taught to always use delegates for UITableViewCell, but I already have a helper class called MyDefaults that handles getting and setting certain preferences to NSUserDefaults.

This is how I'm currently doing it:

In my DummyPreferenceCell.h:

@protocol DummyPreferenceCellDelegate <NSObject>

-(void)setDefaultDummyPreferenceTo:(NSString *)dummyPreferenceOption;

@end

In my DummyPreferenceCell.m:

- (IBAction)dummyPreferenceOptionSelected:(UISegmentedControl *)sender {
    NSString *dummyPreferenceOption = @"";
    switch (sender.selectedSegmentIndex) {
        // first dummy preference option selected
        case 0:
            dummyPreferenceOption = @"dummyOption1";
            break;

        // second dummy preference option selected    
        case 1:
            dummyPreferenceOption = @"dummyOption2";
            break;

        default:
            NSLog(@"ERROR");
            return;
            break;
    }
    [self.delegate setDefaultDummyPreferenceTo:dummyPreferenceOption];
}

In my parent ViewController.m:

#pragma mark - DummyPreferenceCellDelegate

-(void)setDefaultDummyPreferenceTo:(NSString *)dummyPreferenceOption {
    [MyDefaults setDummyPreferenceTo:dummyPreferenceOption];
}

Is delegation necessary when I already have a helper class to do the action for me, or will it cause future problems if I don't use delegation?


Solution

  • Following MVC pattern.

    DummyPreferenceCell extends UITableViewCell which is just part of being View of the Modal View Controller paradigm

    In MVC , the View isnt assigned responsibility to manage the data. that is what the model does. so it is infact correct to pass back the selection to the controller which will pass it to the model and then you can do all validations there