最近做這樣一個需求:如下圖所示鳞上,
中間碰到了一個問題,就是
tableView
調(diào)用reloadData
方法肆糕,但是cellForRowAtIndexPath
不執(zhí)行般堆,因此記錄一下。
一. 例子
//
// ViewController.m
// FJTestProject
//
// Created by fjf on 2017/4/8.
// Copyright ? 2017年 fjf. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
#pragma mark --- life circle
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
self.view.backgroundColor = [UIColor whiteColor];
}
#pragma mark --- system delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 250;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"cellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/256.0 green:arc4random_uniform(256)/256.0 blue:arc4random_uniform(256)/256.0 alpha:1];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 200;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView reloadData];
}
#pragma mark --- private method
// tableView
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor clearColor];
}
return _tableView;
}
@end
這個例子效果如下:
每個cell
的背景色是隨機(jī)的诚啃,當(dāng)點(diǎn)擊任何一個cell
會調(diào)用[self.tableView reloadData]
郁妈,這樣每個cell
的背景色就會更換。
但是當(dāng)加上下面這段代碼:
#pragma mark --- init method
- (instancetype)init {
if (self = [super init]) {
self.tableView.backgroundColor = [UIColor whiteColor];
}
return self;
}
再次點(diǎn)擊cell
調(diào)用[self.tableView reloadData]
绍申,但不會執(zhí)行- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
噩咪;對應(yīng)的cell
的背景色也不會變化顾彰。
同樣的我們給ViewController
添加一個UIColor
的屬性,然后通過這個屬性設(shè)置self.tableView
的背景色
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) UIColor *backgroudColor;
@end
#pragma mark --- setter method
- (void)setBackgroudColor:(UIColor *)backgroudColor {
self.tableView.backgroundColor = backgroudColor;
}
然后在AppDelegate
里面調(diào)用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *tmpViewController = [[ViewController alloc] init];
tmpViewController.backgroudColor = [UIColor redColor];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:tmpViewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
效果如下:
同樣的,再次點(diǎn)擊cell
調(diào)用[self.tableView reloadData]
胃碾,但不會執(zhí)行- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
涨享;對應(yīng)的cell
的背景色也不會變化。
也就是說只要是在- (void)viewDidLoad
之前或者更確切的說是- (void)loadView
之前調(diào)用self.tableView
仆百,這樣就會導(dǎo)致調(diào)用[self.tableView reloadData]
厕隧,不會執(zhí)行- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
;
所以最好保持UIViewController
里面的控件的調(diào)用最好是在- (void)viewDidLoad
方法里面或者之后調(diào)用俄周。
如果非要在- (void)viewDidLoad
之前或者- (void)loadView
之前調(diào)用吁讨,最好先調(diào)用一下self.view
這樣就會保證先去調(diào)用- (void)loadView
,方法這樣也可以保證[self.tableView reloadData]
不會受到影響峦朗。
#pragma mark --- setter method
- (void)setBackgroudColor:(UIColor *)backgroudColor {
self.view.backgroundColor = backgroudColor;
self.tableView.backgroundColor = backgroudColor;
}
效果如下:
二. 原因
具體導(dǎo)致這個問題產(chǎn)生的原因建丧,我也找了很久也沒找到合理的解釋,希望懂得的朋友能給出您寶貴的看法波势。
三.最后
送上一張圖片: