首页 > UITableViewDelgate和UITableViewDataSource的调用顺序问题

UITableViewDelgate和UITableViewDataSource的调用顺序问题

今天学到UITableView,要弄一个自定义UITableViewCell,viewController中有一个自定义行高的协议方法,需要根据自定义的控件的大小来动态设置,可是我并没有给那些自定义的控件初始化,它是怎么赋值的?还是说它已经初始化了?

viewController.m

**- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BLCLStatusTableViewCell *cell = _statusCells[indexPath.row];
    cell.status = _status[indexPath.row];
    // NSLog(@"%f", cell.height);
    return cell.height;
}**

``
整个代码中只有这里使用了自定义控件的初始化:

viewController.m

**- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIndetifier = @"UITableCellIndetifierKey1";
    // 
    BLCLStatusTableViewCell *cell;
    cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIndetifier];
    if (!cell) {
    // 只有这里使用了初始化控件的方法
        cell = [[BLCLStatusTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetifier];
    }
    BLCLStatus *status = _status[indexPath.row];
    // cell.status = status;
    [cell setStatus:status];
    return cell;
}**

在我把初始化方法给注释掉后,最后运行出来界面什么都没有。

如果不去掉的话,界面运行都很正常,
当我把每个方法里都加了句输出,最后的结果在下边:

2015-08-21 20:39:56.991 UITableView(2)[2816:352463] preferredStatusBarStyle 
2015-08-21 20:39:57.063 UITableView(2)[2816:352463] numberOfSectionsInTableView
2015-08-21 20:39:57.063 UITableView(2)[2816:352463] tableView:numberOfRowsInSection:
2015-08-21 20:39:57.074 UITableView(2)[2816:352463] 86.702000
                    ... ...这中间都是返回的行高
2015-08-21 20:39:57.083 UITableView(2)[2816:352463] numberOfSectionsInTableView
2015-08-21 20:39:57.083 UITableView(2)[2816:352463] tableView:numberOfRowsInSection:
2015-08-21 20:39:57.083 UITableView(2)[2816:352463] 86.702000
                ... ...
2015-08-21 20:39:57.093 UITableView(2)[2816:352463] numberOfSectionsInTableView
2015-08-21 20:39:57.093 UITableView(2)[2816:352463] tableView:numberOfRowsInSection:
2015-08-21 20:39:57.094 UITableView(2)[2816:352463] 86.702000
                ... ...
2015-08-21 21:03:26.369 UITableView[2880:364806] tableView:cellForRowAtIndexPath:
2015-08-21 21:03:26.370 UITableView[2880:364806] 86.702000
                ... ... 中间是 tableView:cellForRowAtIndexPath: 和 自定义行高
2015-08-21 21:03:26.401 UITableView[2880:364806] tableView:cellForRowAtIndexPath:
2015-08-21 21:03:26.403 UITableView[2880:364806] 86.702000

只有最后边才是他们两个互相交叉着进行,前边根本就没有 tableView:cellForRowAtIndexPath:的影子,

我的疑问是,这个有初始化方法的 tableView:cellForRowAtIndexPath: 方法怎么会影响到计算行高的方法呢?

那前边的行高是怎么算出来的(没有使用 tableView:cellForRowAtIndexPath 对自定义控件初始化)?

我在网上搜了下,有一篇以前的回答说得是UITableViewDelgate和UITableViewDataSource的方法触发时机视方法不同为异,不存在先后问题,
好吧,又回到我的问题上了,如果是根据情况触发的,可我则也没触发,它怎么会得到结果呢?


1._statusCells[indexPath.row]; 你的cell 是从这个数组里取出来的,你的数组怎么设置的值呢?

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