源码-0203-cell的循环利用01

 

 

 

//
//  ViewController.m
//  01-性能优化
#import "ViewController.h"

@interface ViewController () <UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
//    tableView.frame = self.view.bounds;
    tableView.dataSource = self;
    // 行高
    tableView.rowHeight = 70;
    [self.view addSubview:tableView];
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 50;
}

/**
 *  什么时候调用:每当有一个cell进入视野范围内就会调用
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 0.重用标识
    // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
    static NSString *ID = @"cell";
    
    // 1.先根据cell的标识去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.如果cell为nil(缓存池找不到对应的cell)
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    // 3.覆盖数据
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
    
    return cell;
}
@end

 

//
//  ViewController.m
//  06-UITableView02-单组数据
#import "ViewController.h"
#import "XMGHero.h"

@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/** 英雄数据 */
@property (nonatomic, strong) NSArray *heroes;
@end

@implementation ViewController

- (NSArray *)heroes
{
    if (_heroes == nil) {
        // 加载plist中的字典数组
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heroes.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 字典数组 -> 模型数组
        NSMutableArray *heroArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            XMGHero *hero = [XMGHero heroWithDict:dict];
            [heroArray addObject:hero];
        }
        
        _heroes = heroArray;
    }
    return _heroes;
}

// 定义重用标识
NSString *ID = @"hero";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 注册某个标识对应的cell类型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.heroes.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.去缓存池中查找cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.覆盖数据
    XMGHero *hero = self.heroes[indexPath.row];
    cell.textLabel.text = hero.name;
    cell.imageView.image = [UIImage imageNamed:hero.icon];
    cell.detailTextLabel.text = hero.intro;
    
    return cell;
}
@end

 

 

//
//  XMGHero.h
//  06-UITableView02-单组数据
#import <Foundation/Foundation.h>

@interface XMGHero : NSObject
/** 姓名 */
@property (nonatomic, strong) NSString *name;
/** 图标 */
@property (nonatomic, strong) NSString *icon;
/** 简介 */
@property (nonatomic, strong) NSString *intro;

+ (instancetype)heroWithDict:(NSDictionary *)dict;
@end
//
//  XMGHero.m
//  06-UITableView02-单组数据
#import "XMGHero.h"

@implementation XMGHero
+ (instancetype)heroWithDict:(NSDictionary *)dict
{
    XMGHero *hero = [[self alloc] init];
    hero.name = dict[@"name"];
    hero.icon = dict[@"icon"];
    hero.intro = dict[@"intro"];
    return hero;
}
@end

 

posted @ 2017-02-23 12:46  laugh  阅读(191)  评论(0编辑  收藏  举报