将viewModel设置为表视图数据源是错误的吗?

Suh*_*hal 10 design-patterns mvvm uitableview ios swift

我见过很多代码,其中将ViewModel设置为表视图数据源,而许多代码都没有.

1.将数据源设置到ViewModel一段时间是有意义的,因为数据源方法主要处理表示逻辑.

2.另一方面,将ViewModel设置为数据源意味着您正在实现cellForRowAtIndexPath等,这使得它不独立于UIKit.

构建应用程序的最佳方法是什么,请澄清一下?

小智 2

答案是,没有构建应用程序的最佳方法。有很多好方法可以根据您的需要组织课程。以下是我如何组织 viewModel 以在表视图中显示数据的示例:

PaymentSectionItem 是我的 ViewModel

PaymentSectionItem.h

@interface PaymentSectionItem : NSObject

@property (assign, nonatomic) NSUInteger itemID;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSArray *elements;
@property (assign, nonatomic) kTransactionType transactionType;

+ (NSArray<PaymentSectionItem *> *)allSectionItemsWithData;

@end
Run Code Online (Sandbox Code Playgroud)

PaymentSectionItem.m

@implementation PaymentSectionItem

#pragma mark - Custom Accessors

- (NSString *)localizedTitle {
    NSString *title = [NSString stringWithFormat:@"%@_section_title", self.name];
    return NSLocalizedString(title, @"Section title");
}

- (NSString *)localizedDescription {
    NSString *title = [NSString stringWithFormat:@"%@_section_description", self.name];
    return NSLocalizedString(title, @"Section description");
}

#pragma mark - Constructor

- (instancetype)initWithSectionItem:(kSectionItem)sectionItem {
    self = [super init];
    if (self) {
        [self setupFromHomeSectionItem:sectionItem];
    }
    return self;
}

#pragma mark - Private

- (void)setupFromHomeSectionItem:(kSectionItem)sectionItem {
    self.itemID = sectionItem;
    switch (sectionItem) {
        case kSectionItem1: {
            self.name = @"phones";
            self.elements = [Payment findPaymentsType1];
            break;
        }
        case kSectionItem2: {
            self.name = @"autopay";
            self.elements = [Payment findPaymentsType2];
            break;
        }
        case kSectionItem3: {
            self.name = @"trustfund";
            self.elements = [Payment findPaymentsType3];
            self.transactionType = kTransactionTypeTrustFund;
            break;
        }
        case kSectionItem4: {
            self.name = @"debitlink";
            self.elements = [Payment findPaymentsType4];
            self.transactionType = kTransactionTypeDebitLink;
            break;
        }
        case kSectionItem5: {
            self.name = @"pindebit";
            self.elements = [Payment findPaymentsType5];
            self.transactionType = kTransactionTypePINDebit;
            break;
        }
    }
}

#pragma mark - Public

+ (NSArray<PaymentSectionItem *> *)allSectionItemsWithData {
    NSMutableArray *items = [NSMutableArray new];
    [items addObject:[[PaymentSectionItem alloc] initWithSectionItem:kSectionItem1]];
    [items addObject:[[PaymentSectionItem alloc] initWithSectionItem:kSectionItem2]];
    [items addObject:[[PaymentSectionItem alloc] initWithSectionItem:kSectionItem3]];
    [items addObject:[[PaymentSectionItem alloc] initWithSectionItem:kSectionItem4]];
    [items addObject:[[PaymentSectionItem alloc] initWithSectionItem:kSectionItem5]];
    return items;
}
Run Code Online (Sandbox Code Playgroud)

视图控制器.h

- (void)viewDidLoad {
    [super viewDidLoad];
    self.items = [PaymentSectionItem allSectionItemsWithData];
}



#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.items.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items[section].elements.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GTLAutoPaySectionItem *sectionItem = self.items[indexPath.section];
    NSString *identifier = [self identifierForSectionItem:sectionItem atIndex:indexPath];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    UITableViewCell<PaymentCellProtocol> *paymentCell = (UITableViewCell<PaymentCellProtocol> *)cell;
    [paymentCell setupCellFromValue:sectionItem.elements[indexPath.row] withSectionItem:sectionItem];
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,在我的例子中,我有一个包含许多部分的表格视图,每个部分都有元素。这正是我在 ViewModel 中所做的。如果您还有其他问题,请随时询问。