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 *[email protected]"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;
}

  

时间: 2024-07-30 21:02:59

iOS UITableViewCell自适应高度的相关文章

【转】UITableViewCell自适应高度 UILabel自适应高度和自动换行

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    // 列寬    CGFloat contentWidth = self.tableView.frame.size.width;    // 用何種字體進行顯示    UIFont *font = [UIFont systemFontOfSize:13];       // 該行要顯示的內容    

UITableViewCell 自适应高度 ios8特性

这篇文章介绍了在一个动态数据的 table view 中,cell 根据 text view 内容的输入实时改变 cell 和 table view 的高度.自动计算 cell 高度的功能使用 iOS 8 才支持的自适应 cell,如果你还不知道 iOS 8 自适应 cell,可以参看这篇文章:iOS 8 自适应 Cell 先上图,我们最终要实现的效果是这样的: 图 1:实时更新 cell 高度 实现上面效果的基本原理是:  在 cell 中设置好 text view 的 autolayout,

iOS 8自适应高度单元格问题

iOS 8 中通过UITableViewAutomaticDimension 常量支持自适应高度的单元格(iOS 7 就要麻烦得多).但是在实际应用中,我们需要注意以下几个问题: 1. 设置好模板单元格的自动布局 模板单元格中,subviews的自动局部必须要能够把单元格撑满.也就是说,iOS 必须能够通过内容的自动布局约束计算出 cell 的高.以下面的单元格为例: cell中有上下两个 Label,上面的Label只有一行文本(lines为1),所以高度在运行时不会改变,但下面的Label是

iOS UI10_cell自适应高度

#import "MainViewController.h" #import "Cell.h" @interface MainViewController ()<UITableViewDataSource, UITableViewDelegate> @property(nonatomic, retain)NSArray *picArr; @property(nonatomic, retain)NSMutableArray *ziArr; @end @im

iOS Label 自适应高度

推荐第二个 测试一,只改变numberOfLines属性,label的高度不会自适应(会有text中的一部分内容称为......) NSString *str = @"jgreijgirjeirgjierjgiu4t9eumctuv5 vtmnvghvmc5v5tgh58tc857y"; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 100)]; label.font = [UIFont s

iOS tableViewCell自适应高度 第三发类库

转自:http://www.cnblogs.com/qianLL/p/5393331.html 在github中有许多大牛封装好的第三发类库,其中有个自适应cell高度的类库 下载地址:https://github.com/gsdios/SDAutoLayout model类 commentsModel #import "JSONModel.h" #import "getCommentData.h" @interface commentsModel : JSONMo

iOS UITableViewableViewCell自适应高度

前两天做了一个项目,中间有遇到一个问题,就是聊天的时候cell高度的问题.这是一个很多前辈都遇到过,并且很完美的解决过的问题.这里主要是记录自己的学习心得.项目中首先想到的是用三方库,可是有问题,遂放弃,自己写一个,但是没有封装.项目地址 UITableView 的属性特征什么的,这里就暂时不做介绍了. 由于聊天内容比较简单,不需要对聊天做出很多操作,只是简单的使用 UILable 进行展示即可.首先我们定义一个模型 JXChatModel // // JXChatModel.h // JXAu

iOS UITableCell自适应高度

列如--展示新闻信息列表. 首先得有一个Model类--New New.h为: // //  News.h //  Cellhight // //  Created by Dubai on 15-5-7. //  Copyright (c) 2015年 Dubai. All rights reserved. // #import <Foundation/Foundation.h> @interface News : NSObject @property (nonatomic,retain) N

iOS UITextView自适应高度UITextContainerView抖动问题

在打造一个类似于微信朋友圈评论输入框的时候,需要动态调整输入框的高度, 但是,在调整了UITextView的高度之后,继续输入会导致内容(UITextContainerView里的文字)抖动. scrollRangeToVisible 方法解决了我的问题(Swift 3): textView.scrollRangeToVisible(NSRange.init(location: 0, length: 0)) 获取UITextView内的文字高度以及行数的方法(Swift 3): let heig