• iOS中UITableView--(从plist读取数据到model,实现懒加载, TableView的数据加载)


    #import "RootViewController.h"
    #import "Htohero.h"
    @interface RootViewController ()<UITableViewDataSource>
    @property (nonatomic, retain) NSArray *apps;
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //创建UItableView
        UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        //配置属性
        
        //为tableView指定数据源,想要成为UItableView的数据源的实现UItableView的代理方法
        //让控制成为tableView的数据源
        tableView.dataSource = self;
        
        
        
        //添加俯视图
        [self.view addSubview:tableView];
        //释放
        [tableView release];
    }
    //有几部分
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    //每部分有几行
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.apps.count;
    }
    //每行中显示的内容
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        //cell要实现重用
        //定义一个标志
        static NSString *ID = @"hero";
        //1.通过缓存池寻找可循环利用的cell
        //dequeue :出列 (查找)
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
    
        //获取对象模型
        Htohero *hthero = self.apps[indexPath.row];
        
        cell.imageView.image = hthero.heroimage;
        cell.textLabel.text = hthero.heroName;
        cell.detailTextLabel.text = hthero.heroDis;
        return cell;
    }
    
    
    //把数据导入到模型中,然后导入到定得数组中
    - (NSArray *)apps
    {
        //这个是实现数句的加载,第一次调用这个方法的时候加载全部数据
        //当第二次调用这个方法时不用加载
        //使用懒加载
        if(_apps == nil)
        {
        
        //1.读取heros.plist文件的全路径
       NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
        //2.加载数组,读出plist中的数据
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        //3.将dicArray 中的字典转成模型对象,放到新的数组中
        //遍历dictArray数组
        //4.创建一个可变数组,把模型存到可变数组中
        NSMutableArray *dicArray = [NSMutableArray array];
        for (NSDictionary *dic in dictArray) {
            //初始化模型对象
            Htohero *hero = [Htohero heroWithDic:dic];
            [dicArray addObject:hero];
        }
        //5.赋值
        self.apps = dicArray;
        }
        return _apps;
        
    }
    - (void)dealloc
    {
        [_apps release];
        [super dealloc];
    }
    @end

    Htohero.h

    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    @interface Htohero : NSObject
    @property (nonatomic, copy) NSString *heroName;
    @property (nonatomic, copy) NSString *heroDis;
    @property (nonatomic, retain) UIImage *heroimage;
    
    //自定义初始化
    + (instancetype) heroWithDic:(NSDictionary *)dic;
    - (instancetype) initWithDic:(NSDictionary *)dic;
    @end

    Htohero.m

    #import "Htohero.h"
    
    @implementation Htohero
    + (instancetype) heroWithDic:(NSDictionary *)dic
    {
        return [[self alloc] initWithDic:dic];
    }
    - (instancetype) initWithDic:(NSDictionary *)dic{
        if (self = [super init]) {
            //KVC 字典转模型,这个不行,因为对象中的数据类型和dic中的数据类型不同
            //KVC 通过字符串名名字找到字符串
            //[self setValuesForKeysWithDictionary:dic];
            self.heroimage = [UIImage imageNamed:dic[@"icon"]];
            self.heroDis = dic[@"intro"];
            self.heroName = dic[@"name"];
            
        }
        return self;
    }
    
    - (void)dealloc{
        [_heroDis release];
        [_heroName release];
        [_heroimage release];
        [super dealloc];
    }
    @end
  • 相关阅读:
    深入浅出之正则表达式(二)
    ==和equlas的区别
    温故知新 javascript 正则表达式
    SQL Server高级内容之子查询和表链接
    C语言访问webservice小例子
    jquery菜单伸缩效果
    电影资源下载网站推荐
    百度云使用迅雷下载资源
    字符编码
    7_2 利用 const 引用避免复制
  • 原文地址:https://www.cnblogs.com/wohaoxue/p/4782354.html
Copyright © 2020-2023  润新知