iOS开发中UITableview控件的基本使用及性能优化方法

2020-01-14 17:53:41于海丽
易采站长站为您分析iOS开发中UITableview控件的基本使用及性能优化方法,代码基于传统的Objective-C,需要的朋友可以参考下  

UITableview控件基本使用

一、一个简单的英雄展示程序

NJHero.h文件代码(字典转模型)

复制代码
#import <Foundation/Foundation.h>

 

@interface NJHero : 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

NJViewController.m文件代码

#import "NJViewController.h"
#import "NJHero.h"

@interface NJViewController ()<UITableViewDataSource, UITableViewDelegate>
/**
 *  保存所有的英雄数据
 */
@property (nonatomic, strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end


复制代码
@implementation NJViewController

 

#pragma mark - 懒加载
- (NSArray *)heros
{
    if (_heros == nil) {
        // 1.获得全路径
        NSString *fullPath =  [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];
        // 2.更具全路径加载数据
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
        // 3.字典转模型
        NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
        for (NSDictionary *dict in dictArray) {
            NJHero *hero = [NJHero heroWithDict:dict];
            [models addObject:hero];
        }