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

2020-01-14 17:53:41于海丽

四、cell的优化代码

 代码示例:

复制代码
#import "NJViewController.h"
#import "NJHero.h"

 

// #define ID @"ABC"

@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];
        }
        // 4.赋值数据
        _heros = [models copy];
    }
    // 4.返回数据
    return _heros;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 设置Cell的高度
    // 当每一行的cell高度一致的时候使用属性设置cell的高度
    self.tableView.rowHeight = 160;
}

#pragma mark - UITableViewDataSource
// 返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
// 返回每一组有多少行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section