//// ViewController.m
// Test
//// Created by apple on 2021/2/23.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_dataSource = [NSMutableArray arrayWithArray:@[@"",@""]];
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.delegate = self;
_tableView.dataSource = self;
_footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 50)];
_footerView.backgroundColor = [UIColor blackColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_tapAction)];
[_footerView addGestureRecognizer:tap];
_tableView.tableFooterView = _footerView;
[self.view addSubview:_tableView];
[_tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"-cellForRowAtIndexPath--%ld",indexPath.row);
static NSString *cellId = @"celllId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
cell.backgroundColor = [UIColor redColor];return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"即将展示-willDisplayCell--%ld",indexPath.row);
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
NSLog(@"已经展示-didEndDisplayingCell--%ld",indexPath.row);
}
- (void)_tapAction {
if (_dataSource.count>0) {
[_dataSource removeLastObject];
[_tableView reloadData];
}
}
@end
对于滚动视图
单个item的生命周期
1.cellForRowAtIndexPath 正常cell赋值处理
2.will display cell展示出来之前最后可处理的时候
3.didEndDisplaying 结束展示(从当前视图消失时候,如手动滚动)
如果数据源发生改变 重新reloadData
1.didEndDisplaying
2.cellForRowAtIndexPath 正常cell赋值处理
3.will display cell展示出来之前最后可处理的时候
4.didEndDisplaying 结束展示(从当前视图消失时候,如手动滚动)
结论:
1.正常是在cellForRowAtIndexPath赋值处理
2.如果在didEndDisplaying 修改了数据源 重新reloadData 则会先调用didEndDisplaying,接下来cellForRowAtIndexPath 正常cell赋值处理,其余和【单个item的生命周期】一样