想知道tableview的datasource和delegate调用顺序在时间上存在什么样的关系,和什么有关。 看过这里说http://www.cocoachina.com/bbs/simple/?t81462.html这两个调用之间有时间间隔,为什么会有时间间隔,这个时间间隔和什么有关?
想知道tableview的datasource和delegate调用顺序在时间上存在什么样的关系,和什么有关。 看过这里说http://www.cocoachina.com/bbs/simple/?t81462.html这两个调用之间有时间间隔,为什么会有时间间隔,这个时间间隔和什么有关?
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return dataSource.count;
}这个函数一共会调用四次,刚开始调用一次,数据源改变时调用三次,具体你查断点看看就明白了
2 回答1.1k 阅读
1 回答1.2k 阅读✓ 已解决
1 回答2.8k 阅读
1 回答1.4k 阅读
1.7k 阅读
1 回答949 阅读
1 回答789 阅读
不是我想喷。。。但是我真的得说一句,逻辑不清楚还出来发文写书,真是。。。哎。。。 解释这个问题之前,先明确概念,一个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