扫码关注官方订阅号
84669人学习
65727人学习
82984人学习
467778人学习
498837人学习
471966人学习
256484人学习
152542人学习
224170人学习
139536人学习
81804人学习
85022人学习
11944人学习
20001人学习
60816人学习
5487人学习
15007人学习
2150人学习
6980人学习
194925人学习
359900人学习
1142人学习
19058人学习
3206人学习
180550人学习
48569人学习
17603人学习
40936人学习
1049人学习
750人学习
32909人学习
想知道tableview的datasource和delegate调用顺序在时间上存在什么样的关系,和什么有关。 看过这里说http://www.cocoachina.com/bbs/simple/?t81462.html这两个调用之间有时间间隔,为什么会有时间间隔,这个时间间隔和什么有关?
小伙看你根骨奇佳,潜力无限,来学PHP伐。
不是我想喷。。。但是我真的得说一句,逻辑不清楚还出来发文写书,真是。。。哎。。。 解释这个问题之前,先明确概念,一个tableView的渲染有三个要素:
cocoachina作者所说的DataSource改变指的是用来存放数据的那个dataSource容器发生了变化,这个改变与对tableView对象调用reload方法之间存在时间差就会出问题。举个例子:
// 定义dataSource NSArray *dataSource = @[@"1", @"2", @"3", @"4"];
// rows,渲染时调用一次,只有一次哦 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return dataSource.count; }
// 渲染Cell,决定于numberOfRows - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... cell.textLabel.title = dataSource[indexPath.row]; ... }
// 我改了dataSource dataSource = @[@"11", @"22"]; // 但是不调用Reload
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... // 再渲染到这里的时候,还是按照 4 行在渲染的,当然就挂了! cell.textLabel.title = dataSource[indexPath.row]; ... }
UITableViewDelegate和UITableViewDataSource两组方法各有各的用处,触发时机也视方法不同而异,根本不存在谁先谁后的问题。
代码:
import "DMAppDelegate.h" @interface DMAppDelegate () @property (strong, nonatomic) UITableView *tableView; @property (assign, atomic) NSInteger sn;
@interface DMAppDelegate ()
@property (strong, nonatomic) UITableView *tableView; @property (assign, atomic) NSInteger sn;
@end
@implementation DMAppDelegate
(void)logSN:(NSString *)method { NSLog(@"%@ - SN: %d", method, self.sn ++); }
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible];
self.sn = 0; [self logSN:@"start"]; self.tableView = [[UITableView alloc] initWithFrame:self.window.bounds style:UITableViewStyleGrouped]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.window addSubview:self.tableView];
return YES; }
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { [self logSN:@"numberOfSectionsInTableView"]; return 2; }
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { [self logSN:@"tableView:tableView numberOfRowsInSection:section"]; return 2; }
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [self logSN:@"tableView:tableView cellForRowAtIndexPath:indexPath"]; static NSString *cellIdentifier = @"demo_tableview_cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; }
cell.textLabel.text = [NSString stringWithFormat:@"%d - %d", indexPath.section, indexPath.row];
return cell; }
2013-06-28 17:04:08.948 TBViewDemo[4702:907] start - SN: 0 2013-06-28 17:04:08.950 TBViewDemo[4702:907] Application windows are expected to have a root view controller at the end of application launch 2013-06-28 17:04:08.951 TBViewDemo[4702:907] numberOfSectionsInTableView - SN: 1 2013-06-28 17:04:08.951 TBViewDemo[4702:907] tableView:tableView numberOfRowsInSection:section - SN: 2 2013-06-28 17:04:08.951 TBViewDemo[4702:907] tableView:tableView numberOfRowsInSection:section - SN: 3 2013-06-28 17:04:08.951 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 4 2013-06-28 17:04:08.953 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 5 2013-06-28 17:04:08.954 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 6 2013-06-28 17:04:08.954 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 7
2013-06-28 17:04:18.852 TBViewDemo[4702:907] numberOfSectionsInTableView - SN: 8 2013-06-28 17:04:18.852 TBViewDemo[4702:907] tableView:tableView numberOfRowsInSection:section - SN: 9 2013-06-28 17:04:18.852 TBViewDemo[4702:907] tableView:tableView numberOfRowsInSection:section - SN: 10 2013-06-28 17:04:18.853 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 11 2013-06-28 17:04:18.853 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 12 2013-06-28 17:04:18.853 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 13 2013-06-28 17:04:18.853 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 14
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return dataSource.count;}这个函数一共会调用四次,刚开始调用一次,数据源改变时调用三次,具体你查断点看看就明白了
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号
精品班
技术支持
技术咨询
学习群
会员优惠
返回顶部
不是我想喷。。。但是我真的得说一句,逻辑不清楚还出来发文写书,真是。。。哎。。。 解释这个问题之前,先明确概念,一个tableView的渲染有三个要素:
cocoachina作者所说的DataSource改变指的是用来存放数据的那个dataSource容器发生了变化,这个改变与对tableView对象调用reload方法之间存在时间差就会出问题。举个例子:
UITableViewDelegate和UITableViewDataSource两组方法各有各的用处,触发时机也视方法不同而异,根本不存在谁先谁后的问题。
代码:
@end
@implementation DMAppDelegate
(void)logSN:(NSString *)method { NSLog(@"%@ - SN: %d", method, self.sn ++); }
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible];
self.sn = 0; [self logSN:@"start"]; self.tableView = [[UITableView alloc] initWithFrame:self.window.bounds style:UITableViewStyleGrouped]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.window addSubview:self.tableView];
return YES; }
pragma mark - UITableViewDataSource
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { [self logSN:@"numberOfSectionsInTableView"]; return 2; }
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { [self logSN:@"tableView:tableView numberOfRowsInSection:section"]; return 2; }
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [self logSN:@"tableView:tableView cellForRowAtIndexPath:indexPath"]; static NSString *cellIdentifier = @"demo_tableview_cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (nil == cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; }
cell.textLabel.text = [NSString stringWithFormat:@"%d - %d", indexPath.section, indexPath.row];
return cell; }
pragma mark - UITableViewDelegate
2013-06-28 17:04:18.852 TBViewDemo[4702:907] numberOfSectionsInTableView - SN: 8 2013-06-28 17:04:18.852 TBViewDemo[4702:907] tableView:tableView numberOfRowsInSection:section - SN: 9 2013-06-28 17:04:18.852 TBViewDemo[4702:907] tableView:tableView numberOfRowsInSection:section - SN: 10 2013-06-28 17:04:18.853 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 11 2013-06-28 17:04:18.853 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 12 2013-06-28 17:04:18.853 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 13 2013-06-28 17:04:18.853 TBViewDemo[4702:907] tableView:tableView cellForRowAtIndexPath:indexPath - SN: 14
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return dataSource.count;
}这个函数一共会调用四次,刚开始调用一次,数据源改变时调用三次,具体你查断点看看就明白了