IOS UITableView 实现LOL数据展示

本节重点:

了解UItableView 的基本属性

了解KVC... 其实 我也不是很了解




效果图:

效果图



代码示例:

控制器代码:

//
//  SJViewController.m
//  03.LOL Demo
//
//  Created by SJ.abnormal on 15-2-7.
//  Copyright (c) 2015年 SJ.abnormal. All rights reserved.
//

#import "SJViewController.h"
#import "SJHero.h" //导入模型类

@interface SJViewController ()<UITableViewDataSource> //实现协议
@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) NSArray *heroData;
@end

@implementation SJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置数据源代理
    self.tableView.dataSource = self;
}
//必须实现的三个方法
#pragma mark - 设置UITableView显示几组
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - 设置UITableView第几组显示几行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    
    return self.heroData.count; //返回模型数组长度
}

#pragma mark - 设置cell显示的内容
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"hero";
    //取出对顶索引的模型
    SJHero *data = self.heroData[indexPath.row];
    
    //查找缓冲池中的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    //设置文本信息
    cell.textLabel.text = data.name;
    //设置详细信息
    cell.detailTextLabel.text = data.intro;
    //设置图片
    cell.imageView.image = [UIImage imageNamed:data.icon];
    
    //返回cell
    return cell;
}

#pragma mark - tableView 懒加载
- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView =  [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

#pragma mark - tableView 懒加载
- (NSArray *)heroData {
    
    if (!_heroData) {
        //获取文件路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
        //将文件加载到数组中
        NSArray *dataAray = [NSArray arrayWithContentsOfFile:path];
        //创建模型数组 并设置长度
        NSMutableArray *dictArray = [NSMutableArray arrayWithCapacity:dataAray.count];
        //迭代取出数组中的数据,转换成模型
        for (NSDictionary *dict in dataAray) {
            [dictArray addObject:[SJHero heroWithDict:dict]];
        }
        //将转换好的模型数组赋值
        _heroData = [dictArray copy];
    }
    return _heroData;
}


@end

模型声明:

//
//  SJHero.h
//  03.LOL Demo
//
//  Created by SJ.abnormal on 15-2-7.
//  Copyright (c) 2015年 SJ.abnormal. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface SJHero : NSObject
//模型类
@property (nonatomic, copy) NSString *icon; //图标
@property (nonatomic, copy) NSString *name; //名称
@property (nonatomic, copy) NSString *intro; //详细介绍

//工厂方法
- (instancetype) initWithDict: (NSDictionary *) dict;
+ (instancetype) heroWithDict: (NSDictionary *) dict;
@end

模型实现类:


//
//  SJHero.m
//  03.LOL Demo
//
//  Created by SJ.abnormal on 15-2-7.
//  Copyright (c) 2015年 SJ.abnormal. All rights reserved.
//

#import "SJHero.h"

@implementation SJHero


- (instancetype) initWithDict: (NSDictionary *) dict {
    
    if (self = [super init]) {
        //KVC存取
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
+ (instancetype) heroWithDict: (NSDictionary *) dict {
    
    return [[self alloc] initWithDict: dict];
}
@end






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值