zoukankan      html  css  js  c++  java
  • iOS UITableViewCell自适应高度

      在cell.m文件中

      1)初始化方法中:

     self.lalName=[[UILabel alloc] initWithFrame:CGRectMake(71, 5, 250, 40)];
        [self addSubview:self.lalName];
    

     2)创建方法:

    //给用户介绍赋值并且实现自动换行
    -(void)setIntroductionText:(NSString*)text{
        //获得当前cell的高度
        CGRect frame=[self frame];
        //文本赋值
        self.lalName.text=text;
        CGSize size=CGSizeMake(300, 1000);
        //设置label的最大行数
        self.lalName.numberOfLines=10;
        CGSize labelSize=[self.lalName.text sizeWithFont:self.lalName.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];
        self.lalName.frame=CGRectMake(self.lalName.frame.origin.x, self.lalName.frame.origin.y, labelSize.width, labelSize.height) ;
        
        //计算出自适应的高度
        frame.size.height=labelSize.height+20;
        self.frame =frame;
        
    }
    

      

      在ViewController中:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *acell=@"acell";
        TextKitTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:acell];
        if (!cell) {
            cell=[[[NSBundle mainBundle] loadNibNamed:@"TextKitTableViewCell" owner:self options:nil]lastObject];
        }
        
     
        [cell setIntroductionText:@"where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you?where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you?"];
        return cell;
        
    }
    

      当然别忘记了返回给cell高度

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell=[self tableView:self.tableViewTextKit cellForRowAtIndexPath:indexPath];
        NSLog(@"height===%f",cell.frame.size.height);
        return cell.frame.size.height;
    }
    

      

  • 相关阅读:
    携程 去呼呼 Odoo SSO 单点登录
    Spring Boot 2.2 正式发布,大幅性能提升 + Java 13 支持 & Spring Data Moore M2
    全球顶尖AI技术 微软CRM X 销售易CRM Gartner魔力象限
    职场的真相——七句话
    东软 UniEAP SaCa DataViz & Report
    UML建模——活动图(Activity Diagram)
    领域驱动设计(DDD)部分核心概念
    阿里云RPA4.0背后的黑科技
    Open Source In-Memory Computing Platform
    Mac OS X ifconfig命令解释
  • 原文地址:https://www.cnblogs.com/boyuanmeng/p/4389941.html
Copyright © 2011-2022 走看看