zoukankan      html  css  js  c++  java
  • 【iOS知识学习】_iOS动态改变TableView Cell高度

    在做tableView的时候,我们有时候须要依据cell的高度动态来调整。近期在网上看到一段代码不错。跟大家Share一下。

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

    类中获取cell的高度:
        CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX);
        cell.textLabel.text = @"12345678900123456789";
        cell.userInteractionEnabled = NO;
        cell.textLabel.numberOfLines = 0;
        CGSize requiredSize = [cell.textLabel.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];
        CGRect rect = cell.frame;
        rect.size.height = requiredSize.height+5;
        cell.frame = rect;
    这时候获取到了cell的高度,然后在

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

    类中改变cell的高度:
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
        
        NSLog(@"cell height %f",cell.frame.size.height);
    
        return cell.frame.size.height;
    

    这样以来cell的高度就依据cell里label的内容自己主动改变啦。



    其主要出发点就是我有一个label。然后我要把这个label展示出来。我依据字体的大小还有行数来获取一个高度,这样cell的高度就有啦。



  • 相关阅读:
    JS 循环遍历json
    客户端获取ip
    jquery 常用获取值得方法汇总
    C# MATLAB混合编程
    java设计模式之抽象工厂模式学习
    java设计模式之工厂模式学习
    java设计模式之装饰者模式学习
    本周任务
    模仿jquery的data
    js中random的应用
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5350356.html
Copyright © 2011-2022 走看看