我见过许多将 ViewModel 设置为 TableView 数据源的代码,还有很多代码没有。
1. 有时将数据源设置为 ViewModel 是有意义的,因为数据源方法主要处理表示逻辑。
2.另一方面,将 ViewModel 设置为数据源意味着您正在实现 cellForRowAtIndexPath 等,这使得它不独立于 UIKit。
构建应用程序的最佳方式是什么,请澄清一下?
最佳答案
答案是,没有构建应用程序的最佳方法。有很多好方法可以根据您的需要来组织您的类(class)。 下面是我如何组织 viewModel 以在 TableView 中显示数据的示例:
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
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;
}
ViewController.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;
}
如您所见,在我的例子中,我有一个包含许多部分的 TableView ,每个部分都有元素。这正是我在 ViewModel 中所做的。如果您有任何其他问题,请随时提出。
关于ios - 将 viewModel 设置为 TableView 数据源是否错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46442418/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案