ios 开发自定义 cell 行高

第一步:
设置一个预估高度:

 self.tableView.estimatedRowHeight = 100;

这样就不会先去走

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath```

方法,而是先走下面这个创建 cell

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

然后在 cell 要显示的时候再去调用下面这个来改变 cell 的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

第二部:获取指定 cell返回高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //获取当前indexPath中的cell实例,要是没有设置预设高度,走到这里会崩溃
    UITableViewCell *myCell=[self tableView:tableView cellForRowAtIndexPath:indexPath];
    if( myCell == nil ){
        return 0;

    }else{

        NSLog(@"%@",myCell.contentView.subviews );
        BookSortTool *tool = [myCell.contentView viewWithTag:indexPath.section];
        return  tool.frame.size.height;

    }
}

猜你喜欢

转载自blog.csdn.net/li15809284891/article/details/72615232