自定义cell的高度
//
// 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的高度的更多相关文章
- 自定义cell自适应高度
UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...
- 【swift,oc】ios开发中巧用自动布局设置自定义cell的高度
ios开发中,遇到自定义高度不定的cell的时候,我们通常的做法是抽取一个frame类,在frame类中预算好高度,再返回. 但是苹果出来自动布局之后...春天来了!!来看看怎么巧用自动布局设置自定义 ...
- 自定义cell 自适应高度
#pragma mark - 动态计算cell高度 //计算 返回 文本高度 + (CGFloat)calsLabelHeightWithContact:(Contacts *)contact { / ...
- 自定义 cell 自适应高度
#import "CommodityCell.h" #import "UIImageView+WebCache.h" @implementation Commo ...
- iOS开发总结-UITableView 自定义cell和动态计算cell的高度
UITableView cell自定义头文件:shopCell.h#import <UIKit/UIKit.h>@interface shopCell : UITableViewCell@ ...
- Cell自适应高度及自定义cell混合使…
第一部分:UItableViewCellAdaptionForHeight : cell的自适应高度 第二部分:CustomTableViewCell:自定义cell的混合使用(以简单通讯录为例) = ...
- UITableView自定义Cell中,纯代码编程动态获取高度
在UITableView获取高度的代理方法中,经常需要根据实际的模型重新计算每个Cell的高度.直接的做法是在该代理方法中,直接根据模型来返回行高:另 [1]-(CGFloat)tableView:( ...
- iOS - UITextView放在自定义cell里面-自适应高度
textView放在自定义cell里面-自适应高度 1,textView有个属性 scrollEnabled 要设置为NO; 2,设置tableview的时候 添加这两行代码: self.tabl ...
- 通过代码自定义cell(cell的高度不一致,比如微博)
1.新建一个继承自UITableViewCell的类 2.重写initWithStyle:reuseIdentifier:方法 (先要调用父控件的nitWithStyle:reuseIdentifie ...
随机推荐
- mysql技术内幕之常规使用
mysql中:终止语句方法: 1.在语句结尾处,输入分号(:)表示语句到此结束 2.使用\g(意思是go) \G以垂直的方式显示结果,每行显示一个值 数据库:数据库中包含表,对表中数据执行插入,检索, ...
- hdu 1394(线段树) 最小逆序数
http://acm.hdu.edu.cn/showproblem.php?pid=1394 给出一列数组,数组里的数都是从0到n-1的,在依次把第一个数放到最后一位的过程中求最小的逆序数 线段树的应 ...
- mysql 5.7.17 建立主从数据库
---恢复内容开始--- 1.确定主/从数据库地址; 2.将主数据库的database dump出来 windows平台用workbench; 其他可以参考: 这位同仁写的比较详细了.我就不谢了.打开 ...
- Spring 注解原理(三)@Qualifier @Value
Spring 注解原理(三)@Qualifier @Value Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.Aut ...
- UX设计秘诀之注册表单设计,细节决定成败
以下内容由摹客团队翻译整理,仅供学习交流,摹客iDoc是支持智能标注和切图的产品协作设计神器. 说实话,现实生活中,又有多少人会真正喜欢填写表格?显然,并不多.因为填写表单这样的网页或App服务,并非 ...
- 设计模式之生成者模式java源代码
假设要组装一辆自行车,并且自行车就是车轮和车架组成. Builder对应于组装自行车所使用的车轮和车架 ConcreteBuiler对应于自行车的车轮和车架,同时可以返回一辆自行车. Product对 ...
- 使用flask-alchemy 过程中报错KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'
在网上找了很多, 大多数说是必须要给 SQLALCHEMY_TRACK_MODIFICATIONS 一个默认值,尝试修改alchemy 源码,,但是还是不起作用 最后阅读源码 , self.app = ...
- JavaScript学习笔记:基础知识点总结
基础概念 JavaScript(以下简称Js)中数据类型:Number 字符串 布尔值 数组 对象(Js的对象是一组由键值对组成的无序集合) Js中基础概念:变量(概念和Java中变量概念类似 指示某 ...
- JVM新生代到老年代基础了解
JVM区域总体分两类,heap区和非heap区. heap区又分为: - Eden Space(伊甸园). - Survivor Space(幸存者区). - Old Gen(老年代). 1个Eden ...
- spring学习九 spring aop详解
本文来自于:https://www.cnblogs.com/jingzhishen/p/4980551.html AOP(Aspect-Oriented Programming,面向方面编程),可以说 ...