• 源码-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
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    MariaDB · 版本特性 · MariaDB 的 GTID 介绍
    stm8s 中断重复进入
    PCB积累
    链表的创建、增加、删除、改数据、遍历
    百度文库文字下载工具指引
    防倒灌的开关电路
    AD快速从原理图查找pcb中元件
    三目运算符填坑
    嵌入式结构化分层思想
    原码,反码,补码
  • 原文地址:https://www.cnblogs.com/laugh/p/6432943.html
Copyright © 2020-2023  润新知