首页 > UITableview的代理调用顺序的时间间隔

UITableview的代理调用顺序的时间间隔

想知道tableview的datasource和delegate调用顺序在时间上存在什么样的关系,和什么有关。 看过这里说http://www.cocoachina.com/bbs/simple/?t81462.html这两个调用之间有时间间隔,为什么会有时间间隔,这个时间间隔和什么有关?


不是我想喷。。。但是我真的得说一句,逻辑不清楚还出来发文写书,真是。。。哎。。。 解释这个问题之前,先明确概念,一个tableView的渲染有三个要素:

  1. 数据源,一个存放数据的容器,我们叫他 dataSource(注意大小写),往往是数组
  2. UITableViewDataSource,用于定义tableView结构,做最基本的渲染
  3. UITableViewDelegate,用于处理对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 () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) UITableView *tableView; @property (assign, atomic) NSInteger sn;

  • (void)logSN:(NSString *)method;

@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

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView reloadData]; }
打出来的Log:
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


【热门文章】
【热门文章】