#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
self.tableView.rowHeight = 100.f;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"%s",__func__);
return [UIFont familyNames].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%s",__func__);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
cell.textLabel.text = [UIFont familyNames][indexPath.row];
cell.textLabel.font = [UIFont fontWithName:cell.textLabel.text size:20];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSLog(@"%s",__func__);
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%s",__func__);
return random() % 2 ? 100.f : 50.f;
}
//MARK: Lazy Load
- (UITableView *)tableView {
if (_tableView == nil) {
_tableView = [UITableView new];
}
return _tableView;
}
@end
以上代码 numberOfSectionsInTableView
为什么会输出4次,TableView的数据源方法的调用时序是怎么样的?