ios tableview 自动计算cell高度
2016-07-22 10:58
411 查看
效果图:
![](http://img.blog.csdn.net/20160722105757265?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
#import "TFTableViewController.h" @interface TFTableViewCell () @property(nonatomic,copy)NSString *messageText; @end @implementation TFTableViewCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (!self) { return nil; } self.textLabel.adjustsFontSizeToFitWidth = YES; self.textLabel.textColor = [UIColor darkGrayColor]; self.detailTextLabel.font = [UIFont systemFontOfSize:12.0f]; self.detailTextLabel.numberOfLines = 0; self.selectionStyle = UITableViewCellSelectionStyleGray; return self; } - (void)setPost:(NSString *)messageStr { self.messageText = messageStr; self.textLabel.text = @"123"; self.detailTextLabel.text = messageStr; [self setNeedsLayout]; } //自动计算cell的高度 + (CGFloat)heightForCellWithPost:(NSString *)text { return (CGFloat)fmaxf(70.0f, (float)[self detailTextHeight:text] + 45.0f); } //自动计算cell的高度 + (CGFloat)detailTextHeight:(NSString *)text { CGRect rectToFit = [text boundingRectWithSize:CGSizeMake(240.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12.0f]} context:nil]; return rectToFit.size.height; } #pragma mark - UIView - (void)layoutSubviews { [super layoutSubviews]; self.imageView.frame = CGRectMake(10.0f, 10.0f, 50.0f, 50.0f); self.textLabel.frame = CGRectMake(70.0f, 6.0f, 240.0f, 20.0f); CGRect detailTextLabelFrame = CGRectOffset(self.textLabel.frame, 0.0f, 25.0f); CGFloat calculatedHeight = [[self class] detailTextHeight:self.messageText]; detailTextLabelFrame.size.height = calculatedHeight; self.detailTextLabel.frame = detailTextLabelFrame; } @end @interface TFTableViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic, strong)NSArray *messageArray; @property(nonatomic, strong)UITableView *tfTableView; @property(nonatomic,strong)UIRefreshControl *refreshControl; @end @implementation TFTableViewController #pragma mark - life cycle - (void)viewDidLoad { [super viewDidLoad]; self.messageArray = @[@"qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调调",@"qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调",@"qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调调",@"qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调qwesrdtyuiop[ertyuioezrxtcyvuireztrxytcyvuijq阿尔塞知道热血沸腾超高压我色泽都热血沸腾超高压色的染发痛噶色调",@"1234567"]; self.tfTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kDEVICEWIDTH, kDEVICEHEIGHT - 64) style:UITableViewStylePlain]; self.tfTableView.backgroundColor = [UIColor whiteColor]; self.tfTableView.delegate = self; self.tfTableView.dataSource = self; [self.view addSubview:self.tfTableView]; } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(__unused UITableView *)tableView numberOfRowsInSection:(__unused NSInteger)section { return (NSInteger)[self.messageArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; TFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[TFTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } cell.post = self.messageArray[(NSUInteger)indexPath.row]; return cell; } #pragma mark - UITableViewDelegate - (CGFloat)tableView:(__unused UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { //自动计算cell的高度 return [TFTableViewCell heightForCellWithPost:self.messageArray[(NSUInteger)indexPath.row]]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; } @end
相关文章推荐
- iOS 直接使用16进制颜色
- 几句话实现导航栏透明渐变(iOS)
- iOS文本的替换
- iOS-自定义标签及其控件(一)
- iOS开发拓展篇—CoreLocation地理编码
- iOS开发拓展篇—CoreLocation定位服务
- iOS打包生成pem文件
- iOS水波纹动画详解
- 和block循环引用说再见
- iOS基础概念注意点
- textfield 限制长度 iOS
- iOS错误之-Presenting view controllers on detached view controllers is discouraged
- iOS 8 AutoLayout与Size Class
- iOS Crash之NSInternalInconsistencyException
- iOS开发之AFNetworking 3.0.4使用
- CADisplayLink的调用足够精确吗?
- IOS在子线程中使用定时器,将定时器添加至RunLoop中
- iOS复习安排
- iOS 推送证书生成pem
- iOS开发——pch文件添加和使用