场景:如果一个包含头像、名字、自我介绍文案的自定义view在不同的列表cell的contentView中都存在,那么我们每个cell里都要去依赖这个view,但是可能不同列表的数据源模型model是不同的,那么我们需要cell.model = model赋值时,对于这个view而言,就有多个model对象,这样的代码就有点让人受不了,同一个给子控件赋值的操作因为model不同就要做多遍,怎么处理?
由于MVP架构中的P,可以实现V和M的解耦,原理是:protocal是针对于view渲染所需要的数据来设置的协议,也就是说view子控件所需要的直接数据都可以在protocal中找到,那协议中的数据从哪里来呢?这个就要从遵守这个协议的model类中获取,在model类的.m文件中,去给协议里的这些字段赋值,当然赋值的依据来自于model的数据(直接赋值或者经过计算后赋值)。
这样看来对于view来说没有依赖model,依赖的是遵循了协议的model,不管是哪个model,只要遵循了协议实现了协议中属性的赋值就可以,此时view中是不需要引入哪个model的头文件的,说明实现了V和M的解耦
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@protocol SJTableBaseModelProtocol <NSObject>
//这里都是view中可以直接赋值的一些属性
@required
@property (nonatomic,copy,readonly) NSString *name;
///对于个人的描述
@property (nonatomic,copy,readonly) NSString *content;
@property (nonatomic,copy,readonly) NSString *ageStr;
@property (nonatomic,assign,readonly) BOOL isShow;
@end
NS_ASSUME_NONNULL_END
#import <UIKit/UIKit.h>
#import "SJTableBaseModelProtocol.h"
NS_ASSUME_NONNULL_BEGIN
static CGFloat kBaseCellSubViewLeftMargin = 12.0; //cell内容统一的左边距
@interface SJCellBaseView : UIView
- (void)configWithData:(id <SJTableBaseModelProtocol>)model;
+ (CGFloat)fetchHeightWithData:(id <SJTableBaseModelProtocol>)model;
@end
NS_ASSUME_NONNULL_END
#import "SJCellBaseView.h"
@implementation SJCellBaseView
- (void)configWithData:(id <SJTableBaseModelProtocol>)model {
}
+ (CGFloat)fetchHeightWithData:(id <SJTableBaseModelProtocol>)model {
return 0;
}
@end
@interface SJCellDiyContentView : SJCellBaseView
//点击事件由外部处理
@property (nonatomic, copy) dispatch_block_t clickSeeMoreBlock;
@end
NS_ASSUME_NONNULL_END
@interface SJCellDiyContentView ()
@property (nonatomic, strong) YYLabel *contentLabel;
@end
@implementation SJCellDiyContentView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self configViews];
}
return self;
}
- (void)configViews {
[self addSubview:self.contentLabel];
}
- (void)configWithData:(id < SJTableBaseModelProtocol >)model {
//赋值渲染操作
...
//赋值的时候直接计算一下自己的高度,供外层控件布局使用
self.height = [[self class] fetchHeightWithData:model];
}
+ (CGFloat)fetchHeightWithData:(id < SJTableBaseModelProtocol >)model {
//根据model赋值完,计算控件的高度
return 40;
}
#pragma mark - Getter
- (YYLabel *)contentLabel {
if (!_contentLabel) {
_contentLabel = [[YYLabel alloc] init];
_contentLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
_contentLabel.displaysAsynchronously = YES;
_contentLabel.ignoreCommonProperties = YES;
_contentLabel.fadeOnAsynchronouslyDisplay = NO;
_contentLabel.fadeOnHighlight = NO;
@weakify(self)
_contentLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
@strongify(self);
[self clickNicknameWithText:text range:range];
};
}
return _contentLabel;
}
@end
后续还有多个可复用view的开发,都继承与SJCellBaseView,这样就可以使用父类- (void)configWithData:(id < SJTableBaseModelProtocol >)model {}和+ (CGFloat)fetchHeightWithData:(id < SJTableBaseModelProtocol >)model {}赋值和获取高度方法
假如我们后续又添加了两个自定义viewSJCellDiyContentView_two SJCellDiyContentView_three
@interface SJBaseTableViewCell : UITableViewCell
- (void)configWithData:(id <SJTableBaseModelProtocol>)model;
+ (CGFloat)fetchHeightWithData:(id < SJTableBaseModelProtocol >)model;
// 子类重写此方法,配置好所有模块,按照从上到下的顺序
+ (NSArray *)myComponents;
#pragma mark - 业务交互方法,Cell子类实现
- (void)didClickHead;
- (void)didClickCancleAdvertising;
...
@interface SJBaseTableViewCell ()
@property (nonatomic, strong) NSArray *cellComponents;
//将相似tableviewcell中的控件都放在基础cell中,根据子类的cellComponents方法中添加子控件种类来添加子控件
@property (nonatomic, strong) SJCellDiyContentView *cellDiyContentView;
@property (nonatomic, strong) SJCellDiyContentView_two * cellDiyContentView_two;
@property (nonatomic, strong) SJCellDiyContentView_three * cellDiyContentView_three;
@property (nonatomic, strong) UIView *bottomView;
@end
@implementation SJBaseTableViewCell
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]){
self.cellComponents = [[self class] myComponents];
[self configViews];
}
return self;
}
- (void)configViews {
//子类中通过myComponents方法中添加枚举值得方式,添加子视图,在父视图这里择情况从上往下添加子视图
//继承与该cell的子类可以包含多种组合方式
for (int i = 0; i < [self.cellComponents count]; i++) {
NSNumber *num = [self.cellComponents objectAtIndex:i];
switch (num.integerValue) {
//自定义view的枚举值
case kSJCellDiyContentView:
[self.contentView addSubview:self.cellDiyContentView];
break;
//自定义view2的枚举值
case kSJCellDiyContentView_two:
[self.contentView addSubview:self.cellDiyContentView_two];
break;
//自定义view3的枚举值
case kSJCellDiyContentView_three:
[self.contentView addSubview:self.cellDiyContentView_three];
break;
default:
break;
}
}
//self.bottomView用于封底
[self.contentView addSubview:self.bottomView];
[self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self.contentView);
make.height.equalTo(@(10));
}];
}
- (void)configWithData:(id < SJTableBaseModelProtocol >)model {
CGFloat bottom = 0;
for (int i = 0; i < [self.cellComponents count]; i++) {
NSNumber *num = [self.cellComponents objectAtIndex:i];
switch (num.integerValue) {
//自定义view的枚举值
case kSJCellDiyContentView:
[self.cellDiyContentView configWithData:model];
self.cellDiyContentView.frame = CGRectMake(0, bottom, kScreenWidth, self.cellRecommendHeadView.height);
bottom = bottom + self.cellDiyContentView.height;
break;
//自定义view2的枚举值
case kSJCellDiyContentView_two:
[self.cellDiyContentView_two configWithData:model];
self.cellDiyContentView_two.frame = CGRectMake(0, bottom, kScreenWidth, self.cellDiyContentView_two.height);
bottom = bottom + self.cellDiyContentView_two.height;
break;
//自定义view3的枚举值
case kSJCellDiyContentView_three:
[self.cellDiyContentView_three configWithData:model];
self.cellDiyContentView_three.frame = CGRectMake(0, bottom, kScreenWidth, self.cellDiyContentView_three.height);
bottom = bottom + self.cellDiyContentView_three.height;
break;
default:
break;
}
}
}
+ (CGFloat)fetchHeightWithData:(id < SJTableBaseModelProtocol >)model {
CGFloat height = 0;
for (int i = 0; i < [[[self class] myComponents] count]; i++) {
NSNumber *num = [[[self class] myComponents] objectAtIndex:i];
switch (num.integerValue) {
//自定义view的枚举值
case kSJCellDiyContentView:
height = height + [SJCellDiyContentView fetchHeightWithData:model];
break;
//自定义view2的枚举值
case kSJCellDiyContentView_two:
height = height + [SJCellDiyContentView_two fetchHeightWithData:model];
break;
//自定义view3的枚举值
case kSJCellDiyContentView_three:
height = height + [SJCellDiyContentView_three fetchHeightWithData:model];
break;
default:
break;
}
}
height = height + 10 + 15; // 10 为self.bottomView 高度,15 为距离self.bottom 顶部的间距
return height;
}
#pragma mark - lazyload
子控件的懒加载区域
#pragma mark - 交互方法,子类实现
+ (NSArray *)myComponents {
return @[];
}
//view中的点击事件在本类中先不实现,对外提供接口,让子类实现
- (void)didClickHead {}
- (void)didClickCancleAdvertising {}
使用 :我们有一个cell样式是由
SJCellDiyContentView_two和SJCellDiyContentView_three组合的
customTableviewCell继承自SJBaseTableViewCell
NS_ASSUME_NONNULL_BEGIN
@interface customTableviewCell : SJBaseTableViewCell
@end
NS_ASSUME_NONNULL_END
@implementation customTableviewCell
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupData];
}
return self;
}
#pragma mark - 本cell包含的所有模块
+ (NSArray *)myComponents {
//对于父类的所有子控件来说,本cell只包含了这两种view,所以只加这两种枚举,从上往下加
return @[@(kSJCellDiyContentView_two),
@(kSJCellDiyContentView_three),];
}
- (void)configWithData:(id<SJTableBaseModelProtocol>)model {
//调用父类的赋值方法
[super configWithData:model];
}
+ (CGFloat)fetchHeightWithData:(id < SJTableBaseModelProtocol >)model{
return [super fetchHeightWithData:model];
}
//这里就可以实现父类中在本cell中的具体触发到的点击事件了
- (void)didClickHead{
//具体实现。。。
}
#import <Foundation/Foundation.h>
#import "SJTableBaseModelProtocol.h"
NS_ASSUME_NONNULL_BEGIN
@interface DiyModel : NSObject< SJTableBaseModelProtocol >
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *content;
@property (nonatomic,assign) NSInteger age;
@end
NS_ASSUME_NONNULL_END
#import "DiyModel.h"
@implementation DiyModel
//在类中遵循了代理,就要实现协议里那些属性的赋值,给到协议的值是view可以直接用的
- (NSString *)name{
return [NSString stringWithFormat:@"我的名字叫:%@",_name];
}
- (NSString *)ageStr{
return [NSString stringWithFormat:@"%ld",self.age];
}
//如果不用处理直接赋值的,注意嵌套循环造成死循环,比如这里就不能return self.content,会造成自身一直在嵌套循环
- (NSString *)content{
return _content;
}
- (BOOL)isShow{
if (self.age > 18) {
return YES;
}else{
return NO;
}
}
@end
其他列表cell的数据源不同,只要也遵循协议,并把协议的属性通过自己model的属性经过处理赋值,那么就还是可以用户我们的view渲染试图方法- (void)configWithData:(id<SJTableBaseModelProtocol>)model因为我们要的只是遵循了SJTableBaseModelProtocol的东西,不关系他是什么
总结:用这种方式,就像堆积木一样,首先cell的子控件是通用的,我基础cell组建里包含了所有种子控件,使用的时候只要通过枚举去添加我们所需要的控件就OK了,然后网络请求好的数据封装model后传值给cell即可
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi
导读:随着叮咚买菜业务的发展,不同的业务场景对数据分析提出了不同的需求,他们希望引入一款实时OLAP数据库,构建一个灵活的多维实时查询和分析的平台,统一数据的接入和查询方案,解决各业务线对数据高效实时查询和精细化运营的需求。经过调研选型,最终引入ApacheDoris作为最终的OLAP分析引擎,Doris作为核心的OLAP引擎支持复杂地分析操作、提供多维的数据视图,在叮咚买菜数十个业务场景中广泛应用。作者|叮咚买菜资深数据工程师韩青叮咚买菜创立于2017年5月,是一家专注美好食物的创业公司。叮咚买菜专注吃的事业,为满足更多人“想吃什么”而努力,通过美好食材的供应、美好滋味的开发以及美食品牌的孵
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o