天天看點

iOS UITableView 傳回的 indexPath.row 不是從 0 開始的

0x00 下标從4開始!?

封裝了一個包含​

​TableView​

​​的視圖

使用的時候

建立了一個子類來用

把這個子類視圖放在了一個嵌套了​​

​多層​

​​的視圖内

一開始發現觸摸視圖時會​​

​抖動​

​ 為何會抖動呢?

在方法​

​tableView:cellForRowAtIndexPath:​

​内列印日志

居然是這種!!

iOS UITableView 傳回的 indexPath.row 不是從 0 開始的

怎麼會直接從​

​4​

​開始!?

0x01 代理方法

之前在​

​控制器​

​内的寫法是:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Model *model = _dataArray[indexPath.row];
    return model.cellHeight;
}
      

​model.cellHeight​

​​是不會為​

​0​

​的

但是,在視圖​

​嵌套​

​​層級比較多的情況下

​​

​model.cellHeight​

​​全是​

​0​

​了

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Model *model = _dataArray[indexPath.row];
    if (model.cellHeight == 0) {
        // 這個代理會給cell的模型指派,并計算cell高度,緩存在model中
        [self tableView:tableView cellForRowAtIndexPath:indexPath]; 
    }
    return model.cellHeight;
}
      

一套輕量級的布局架構