UI基础-day04-Shopping-08Xib-02简单的加载数据

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基本使用

需求

  1. 自定义View是加载进来了,但是没有数据,怎么加载数据呢?

效果图

代码

......
//1.设置临时数据
NSDictionary * dictData = @{@"icon":@"qianbao",@"name":@"钱包"};
HUHShopModel * shopM = [HUHShopModel shopsWithDict:dictData];
//2.获取xib文件,得到UIView控件
NSArray * array = [[NSBundle mainBundle]loadNibNamed:@"HUHShopView" owner:nil options:nil];
UIView * shopView = array.lastObject;

//3.获取子控件,给对应控件设置值
for (UIView *subView in shopView.subviews) { 
    if([subView isKindOfClass:[UIImageView class]]){
        UIImageView * shopImg = (UIImageView *)subView;
        shopImg.image = [UIImage imageNamed:shopM.icon];
    }else if([subView isKindOfClass:[UILabel class]]){
        UILabel *labShop = (UILabel *)subView;
        labShop.text = shopM.name;
        NSLog(@"%@",shopM.name);
    }
}
......

缺陷

  1. for(UIView * subView in shopView.subviews)
    如果Xib文件有很子控件,那么使用遍历则会很消耗性能 .
  2. [subView isKindOfClass:[XX class]] 判断类型是否匹配
    如果有很多相同类型的子控件,那么通过isKindofClass方法,是无法精准的得到,自己想要的控件.

你可能感兴趣的:(UI基础-day04-Shopping-08Xib-02简单的加载数据)