//
//  RootTableViewController.m
//  Share
//
//  Created by lanouhn on 15/1/20.
//  Copyright (c) 2015年 niutiantian. All rights reserved.
//

#import "RootTableViewController.h"
#import "CustomTableViewCell.h"
#import "DetailViewController.h"

static NSString *cellIndentifer = @"cell";

@interface RootTableViewController ()

@end

@implementation RootTableViewController

- (void)dealloc
{
    self.charString = nil;
    self.secondString = nil;
    self.custom = nil;
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.charString = @"只要有吃的,鱼就不停地吃?因为不知饥饱,鱼缸里的鱼因为喂食太多,给撑死了?只要有吃的,鱼就不停地吃?因为不知饥饱,鱼缸里的鱼因为喂食太多,给撑死了?只要有吃的,鱼就不停地吃?因为不知饥饱,鱼缸里的鱼因为喂食太多,给撑死了?";
    self.secondString = @"只要有吃的,鱼就不停地吃?因为不知饥饱,鱼缸里的鱼因为喂食太多,给撑死了?只要有吃的,鱼就不停地吃?因为不知饥饱,鱼缸";
    
        //注册cell
    [self.tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:cellIndentifer];
    self.custom = [self.tableView dequeueReusableCellWithIdentifier:cellIndentifer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer forIndexPath:indexPath];
    if (indexPath.row % 2 == 0) {
        cell.label.text = _charString;
        [cell resetLabelFram:_charString];
        
    } else {
        cell.label.text = _secondString;
        [cell resetLabelFram:_secondString];
    }
        //错误案例
//    if (indexPath.row % 2 == 0) {
//        self.tableView.rowHeight = [CustomTableViewCell heigehtCharString:_charString];
//    } else {
//        self.tableView.rowHeight = [CustomTableViewCell heigehtCharString:_secondString];
//    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        //方法一
//    if (indexPath.row % 2 == 0) {
//       return [CustomTableViewCell heigehtCharString:_charString];
//    } else {
//        return [CustomTableViewCell heigehtCharString:_secondString];
//    }
        //方法二
    if (indexPath.row % 2 == 0) {
        self.custom.label.text = _charString;
       
    } else {
        self.custom.label.text = _secondString;
    
    }
    CGRect rect = [self.custom.label.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
    return rect.size.height;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detail = [[DetailViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}

@end

//
//  CustomTableViewCell.m
//  Share
//
//  Created by lanouhn on 15/1/20.
//  Copyright (c) 2015年 niutiantian. All rights reserved.
//

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        _label.numberOfLines = 0;
        [self addSubview:_label];
        [_label release];
    }
    return self;
}

+ (CGFloat)heigehtCharString: (NSString *)aString
{
    CGRect rect = [aString boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
    return  rect.size.height;
    
}
    //重设label的高度
- (void)resetLabelFram: (NSString *)aString
{
       CGRect rect = [aString boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
    
    _label.frame = CGRectMake(0, 0, self.bounds.size.width, rect.size.height);
}

@end

 

自定义cell的高度的更多相关文章

  1. 自定义cell自适应高度

    UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...

  2. 【swift,oc】ios开发中巧用自动布局设置自定义cell的高度

    ios开发中,遇到自定义高度不定的cell的时候,我们通常的做法是抽取一个frame类,在frame类中预算好高度,再返回. 但是苹果出来自动布局之后...春天来了!!来看看怎么巧用自动布局设置自定义 ...

  3. 自定义cell 自适应高度

    #pragma mark - 动态计算cell高度 //计算 返回 文本高度 + (CGFloat)calsLabelHeightWithContact:(Contacts *)contact { / ...

  4. 自定义 cell 自适应高度

    #import "CommodityCell.h" #import "UIImageView+WebCache.h" @implementation Commo ...

  5. iOS开发总结-UITableView 自定义cell和动态计算cell的高度

    UITableView cell自定义头文件:shopCell.h#import <UIKit/UIKit.h>@interface shopCell : UITableViewCell@ ...

  6. Cell自适应高度及自定义cell混合使…

    第一部分:UItableViewCellAdaptionForHeight : cell的自适应高度 第二部分:CustomTableViewCell:自定义cell的混合使用(以简单通讯录为例) = ...

  7. UITableView自定义Cell中,纯代码编程动态获取高度

    在UITableView获取高度的代理方法中,经常需要根据实际的模型重新计算每个Cell的高度.直接的做法是在该代理方法中,直接根据模型来返回行高:另 [1]-(CGFloat)tableView:( ...

  8. iOS - UITextView放在自定义cell里面-自适应高度

    textView放在自定义cell里面-自适应高度 1,textView有个属性 scrollEnabled  要设置为NO; 2,设置tableview的时候  添加这两行代码: self.tabl ...

  9. 通过代码自定义cell(cell的高度不一致,比如微博)

    1.新建一个继承自UITableViewCell的类 2.重写initWithStyle:reuseIdentifier:方法 (先要调用父控件的nitWithStyle:reuseIdentifie ...

随机推荐

  1. ecplise自动提示失效,使用补全自动提示快捷键(Alt+/),但只显示“No Default Proposals”

    在这里设置了自动提示,但是在使用的时候自动提示实现了.甚至使用补全自动提示快捷键(Alt+/),只显示“No Default Proposals”.今天在网上搜索了一下结果,主要有一下几种方法: 1. ...

  2. PhotoSphereViewer 全景图

    1网站地址:http://photo-sphere-viewer.js.org/markers.html#demo 2参数中文地址:https://www.cnblogs.com/big-tree/p ...

  3. 集合 day8

    一,集合. 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. ...

  4. Sublime Text3 常用快捷键必看

    Sublime Text3 常用快捷键必看  https://blog.csdn.net/md1688/article/details/53043525

  5. Soa思想分布式服务webservice WCF

    什么是分布式事务 分布式事务就是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.以上是百度百科的解释,简单的说,就是一次大的操作由不同的小操作组成,这 ...

  6. 【Android优化篇】提升Activity加载速度的方法

    文章转自:http://www.jianshu.com/p/2007ca0290d3 作者: CoderFan 前言 这个也是我面试遇到的问题,当时只回答了一种情况,异步加载数据,没想到别的方式,回来 ...

  7. linux 使用笔记3

    解决linux下打开txt乱码问题 在Linux下要阅读windows生成的txt文件,需要通过iconv进行字符转化 iconv -f gb2312 -t utf8 ./读书笔记.txt > ...

  8. .net序列化

    在开发过程中,会遇到很多需要使用序列化的场景,比如wcf,webservice或者jquery+.net等.那今天就说说我对序列化的理解. 在.net中有几种序列化的方式,XML.二进制.SOAP.还 ...

  9. VS2010下MFC的串口编程

    串口通信简介 一般来说,计算机都有一个或多个串行端口,这些串口提供了外部设备与PC进行数据传输和通信的通道,在CPU和外设之间充当解释器的角色.当字符数据从CPU发送给外设时,这些字符数据将被转换成串 ...

  10. [Robot Framework] 学习资料

    https://www.cnblogs.com/pachongshangdexuebi/category/981644.html Robot Framework学习笔记(一)------环境搭建 Ro ...