UITableView調(diào)用reloadData方法庐橙,但cellForRowAtIndexPath不執(zhí)行

最近做這樣一個需求:如下圖所示鳞上,

滾動欄.gif

中間碰到了一個問題,就是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

這個例子效果如下:


tableView.gif

每個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的背景色也不會變化顾彰。

tableView-2.gif

同樣的我們給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;
}

效果如下:

tableView-3.gif

同樣的,再次點(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;
}

效果如下:


tableView-4.gif

二. 原因

具體導(dǎo)致這個問題產(chǎn)生的原因建丧,我也找了很久也沒找到合理的解釋,希望懂得的朋友能給出您寶貴的看法波势。

三.最后

送上一張圖片:

圖片發(fā)自簡書App
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末翎朱,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子尺铣,更是在濱河造成了極大的恐慌拴曲,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,946評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件凛忿,死亡現(xiàn)場離奇詭異澈灼,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)店溢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評論 3 399
  • 文/潘曉璐 我一進(jìn)店門叁熔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人逞怨,你說我怎么就攤上這事者疤。” “怎么了叠赦?”我有些...
    開封第一講書人閱讀 169,716評論 0 364
  • 文/不壞的土叔 我叫張陵驹马,是天一觀的道長。 經(jīng)常有香客問我除秀,道長糯累,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,222評論 1 300
  • 正文 為了忘掉前任册踩,我火速辦了婚禮泳姐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘暂吉。我一直安慰自己胖秒,他們只是感情好缎患,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,223評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著阎肝,像睡著了一般挤渔。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上风题,一...
    開封第一講書人閱讀 52,807評論 1 314
  • 那天判导,我揣著相機(jī)與錄音,去河邊找鬼沛硅。 笑死眼刃,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的摇肌。 我是一名探鬼主播擂红,決...
    沈念sama閱讀 41,235評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼朦蕴!你這毒婦竟也來了篮条?” 一聲冷哼從身側(cè)響起弟头,我...
    開封第一講書人閱讀 40,189評論 0 277
  • 序言:老撾萬榮一對情侶失蹤吩抓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后赴恨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體疹娶,經(jīng)...
    沈念sama閱讀 46,712評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,775評論 3 343
  • 正文 我和宋清朗相戀三年伦连,在試婚紗的時候發(fā)現(xiàn)自己被綠了雨饺。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,926評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡惑淳,死狀恐怖额港,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情歧焦,我是刑警寧澤移斩,帶...
    沈念sama閱讀 36,580評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站绢馍,受9級特大地震影響向瓷,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜舰涌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,259評論 3 336
  • 文/蒙蒙 一猖任、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瓷耙,春花似錦朱躺、人聲如沸刁赖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽乾闰。三九已至,卻和暖如春盈滴,著一層夾襖步出監(jiān)牢的瞬間涯肩,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評論 1 274
  • 我被黑心中介騙來泰國打工巢钓, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留病苗,地道東北人。 一個月前我還...
    沈念sama閱讀 49,368評論 3 379
  • 正文 我出身青樓症汹,卻偏偏與公主長得像硫朦,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子背镇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,930評論 2 361

推薦閱讀更多精彩內(nèi)容