UI基础-day04-Shopping-08Xib-05自定义Xib控件-优化

UI基础-day02-Shopping-01搭建商城基本框架
UI基础-day02-Shopping-02添加一个商品
UI基础-day02-Shopping-03九宫格排序
UI基础-day02-Shopping-04优化添加和删除功能
UI基础-day02-Shopping-05加载数据
UI基础-day02-Shopping-06懒加载-1基础
UI基础-day03-Shopping-06懒加载-2Plist文件读取
UI基础-day03-Shopping-06懒加载-3字典转模型
UI基础-day04-Shopping-07自定义控件-01设置子控件的frame
UI基础-day04-Shopping-07自定义控件-02加载数据
UI基础-day04-Shopping-07自定义控件-03初始化和类方法
UI基础-day04-Shopping-08Xib-01基本使用
UI基础-day04-Shopping-08Xib-02简单的加载数据
UI基础-day04-Shopping-08Xib-03Tag值方式加载数据
UI基础-day04-Shopping-08Xib-04自定义Xib控件

优化一

拼写优化,NSStringFromClass(self), 将当前类转换为NSString类型.
结果就是: @"HUHShopView", 避免出现拼写错误,而没有提示的情况.

+(instancetype)shopView{
NSArray * array = [[NSBundle mainBundle]loadNibNamed:@"HUHShopView" owner:nil options:nil];
UIView * shopView = array.firstObject;
return shopView
}

优化为:

实例化Xib
+(instancetype)shopView{
return [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]firstObject];
}

优化二

  1. 通过一个类方法,实现即加载了xib,又给子控件赋值!
+(instancetype)shopViewWithShop:(HUHShopModel *)shop{
    HUHShopView *shopV = [self shopView]; // 实例化Xib
    shopV.shopM = shop; //给子控件赋值
    return shopV;
}

注意: HUHShopView * shopV = [self shopView]不能改成self = [self shopView],避免子类调用了此方法,类型就变成子类了(必须确保是HUHShopView类型),然后就调用子类的-setShopM方法了.

给子控件赋值
-(void)setShopM:(HUHShopModel *)shopM{
    _shopM = shopM;
  self.lblTemp.text = shopM.name;
  self.ImgView.image = [UIImage imageNamed:shopM.icon];
}

注意:_shopM = shopM等于原来的属性并没有变,只是在原来的基础上,增加了给控件赋值.

  1. 在ViewController中使用
- (void)viewDidLoad {
    1.设置临时数据
    ......
    2.封装加载xib,给子控件设置数据.
    HUHShopView *shopV = [HUHShopView shopViewWithShop:shopM];
    [self.view addSubview:shopV];
}

你可能感兴趣的:(UI基础-day04-Shopping-08Xib-05自定义Xib控件-优化)