程序员将来跳槽用iOS收藏

UITableView方法的执行顺序流畅性优化

2017-10-23  本文已影响167人  朝雨晚风

(一)、UITableView的执行顺序

  1. numberOfSectionsInTableView(确定有几组) -> numberOfRowsInSection(确定每组有多少的行) -> heightForRowAtIndexPath(确定每行cell的高度)

  2. 以上信息确定完毕后再依次调用cellForRowAtIndexPath -> heightForRowAtIndexPath

  3. 当滚动获取超出屏幕的cell 会再依次调用 cellForRowAtIndexPath -> heightForRowAtIndexPath

#import "MyTableViewController.h"

@interface MyTableViewController ()

@end

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"viewDidLoad");
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    NSLog(@"viewWillAppear");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
//    NSLog(@"viewDidAppear");
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSLog(@"---------------------------------");
    NSLog(@"numberOfSections");
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"numberOfRows");
    return  10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"我是第 %ld 行",(long)indexPath.row];
    NSLog(@"cellForRowAtIndexPath");
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"heightForRowAtIndexPath");
    return 100;
}

@end

打印结果:

2017-10-23 15:44:30.834 Test[79696:9847728] ---------------------------------
2017-10-23 15:44:30.834 Test[79696:9847728] numberOfSections
2017-10-23 15:44:30.835 Test[79696:9847728] numberOfRows
2017-10-23 15:44:30.835 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.835 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.835 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.835 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.835 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.835 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.835 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.835 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.836 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.836 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.840 Test[79696:9847728] ---------------------------------


2017-10-23 15:44:30.842 Test[79696:9847728] cellForRowAtIndexPath
2017-10-23 15:44:30.842 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.845 Test[79696:9847728] cellForRowAtIndexPath
2017-10-23 15:44:30.845 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.846 Test[79696:9847728] cellForRowAtIndexPath
2017-10-23 15:44:30.846 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.846 Test[79696:9847728] cellForRowAtIndexPath
2017-10-23 15:44:30.846 Test[79696:9847728] heightForRowAtIndexPath
2017-10-23 15:44:30.847 Test[79696:9847728] cellForRowAtIndexPath
2017-10-23 15:44:30.847 Test[79696:9847728] heightForRowAtIndexPath


2017-10-23 15:44:30.849 Test[79696:9847728] ---------------------------------
2017-10-23 15:47:59.648 Test[79696:9847728] cellForRowAtIndexPath
2017-10-23 15:47:59.648 Test[79696:9847728] heightForRowAtIndexPath

UITableView的回调顺序是先多次调用tableView:heightForRowAtIndexPath:以确定contentSize及Cell的位置,然后才会调用tableView:cellForRowAtIndexPath:,从而来显示在当前屏幕的Cell。
举个例子来说:如果现在要显示100个Cell,当前屏幕显示5个。那么刷新(reload)UITableView时,UITableView会先调用100次tableView:heightForRowAtIndexPath:方法,然后调用5次tableView:cellForRowAtIndexPath:方法;滚动屏幕时,每当Cell滚入屏幕,都会调用一次tableView:heightForRowAtIndexPath:、tableView:cellForRowAtIndexPath:方法。

(二)、UITableView的流畅性优化

除了上面最主要的三个方面外,还有很多几乎大伙都很熟知的优化点:

上一篇 下一篇

猜你喜欢

热点阅读