iOS - 当我有 Helper Class 时,是否需要 UITableViewCell 委托?

iOS - Is UITableViewCellDelegate Necessary When I Have Helper Class?

我被教导要始终为 UITableViewCell 使用委托,但我已经有一个名为 MyDefaults 的助手 class 来处理获取和设置某些首选项 NSUserDefaults.

这就是我目前的做法:

在我的 DummyPreferenceCell.h:

@protocol DummyPreferenceCellDelegate <NSObject>

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

@end

在我的 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];
}

在我的 parent ViewController.m:

#pragma mark - DummyPreferenceCellDelegate

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

当我已经有一个帮手class为我执行操作时是否需要委托,或者如果我不使用委托会导致未来出现问题吗?

遵循 MVC 模式。

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

如果您需要 ViewController 中的 UISegmentedControl 值更改时的事件,您需要编写委托,否则您可以将其存储在您的助手 class 中。每当你写委托时,它应该是。

- (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;
    }
   if([self.delegate respondsToSelector:@selector(setDefaultDummyPreferenceTo:) ])
    [self.delegate setDefaultDummyPreferenceTo:dummyPreferenceOption];
}

希望这对您有所帮助