请教tableviewios8 cell自适应高度度的问题

iOS / iPhone 只需3步设置即可实现cell和tableview高度自适应 代码 简单易用的Cell高度自适应 其他(Others)开源代码 -
简单易用的Cell高度自适应
已有 59 人收藏
&&&&只需3步设置即可实现cell和tableview高度自适应
&&&&视频教程:
测试环境:
&&[Code4App]编译测试
代码评论:
登录后方可评论
-控件分类-
-功能分类-1850人阅读
iOS基础知识(18)
普通(简化)版【推荐使用】:tableview 高度自适应设置只需要2步
1. && 设置cell高度自适应:
// cell布局设置好之后调用此方法就可以实现高度自适应(注意:如果用高度自适应则不要再以cell的底边为参照去布局其子view)
[cell setupAutoHeightWithBottomView:_view4 bottomMargin:10];
2. && 获取自动计算出的cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
id model = self.modelsArray[indexPath.row];
// 获取cell高度
return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@&model& cellClass:[DemoVC9Cell class]
contentViewWidth:cellContentViewWith];
}升级版(适应于cell条数少于100的tableview):tableview 高度自适应设置只需要2步1. && 设置cell高度自适应:
// cell布局设置好之后调用此方法就可以实现高度自适应(注意:如果用高度自适应则不要再以cell的底边为参照去布局其子view)
[cell setupAutoHeightWithBottomView:_view4 bottomMargin:10];
2. && 获取自动计算出的cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
// 获取cell高度
return [self cellHeightForIndexPath:indexPath cellContentViewWidth:[UIScreen mainScreen].bounds.size.width];
}普通view的自动布局:用法示例/* 用法一 */
_view.sd_layout
.leftSpaceToView(self.view, 10)
.topSpaceToView(self.view, 80)
.heightIs(130)
.widthRatioToView(self.view, 0.4);
/* 用法二 (一行代码搞定,其实用法一也是一行代码) */
_view.sd_layout.leftSpaceToView(self.view, 10).topSpaceToView(self.view,80).heightIs(130).widthRatioToView(self.view, 0.4);
&& UILabel文字自适应:
// autoHeightRatio() 传0则根据文字自动计算高度(传大于0的值则根据此数值设置高度和宽度的比值)
_label.sd_layout.autoHeightRatio(0);
*******************************************************************************
注意:先把需要自动布局的view加入父view然后在进行自动布局,例:
UIView *view0 = [UIView new];
UIView *view1 = [UIView new];
[self.view addSubview:view0];
[self.view addSubview:view1];
view0.sd_layout
.leftSpaceToView(self.view, 10)
.topSpaceToView(self.view, 80)
.heightIs(100)
.widthRatioToView(self.view, 0.4);
view1.sd_layout
.leftSpaceToView(view0, 10)
.topEqualToView(view0)
.heightRatioToView(view0, 1)
.rightSpaceToView(self.view, 10);
*******************************************************************************自动布局用法简析1.1 & leftSpaceToView(self.view, 10)方法名中带有“SpaceToView”的方法表示到某个参照view的间距,需要传递2个参数:(UIView)参照view 和 (CGFloat)间距数值1.2 & widthRatioToView(self.view, 1)方法名中带有“RatioToView”的方法表示view的宽度或者高度等属性相对于参照view的对应属性值的比例,需要传递2个参数:(UIView)参照view 和 (CGFloat)倍数1.3 & topEqualToView(view)方法名中带有“EqualToView”的方法表示view的某一属性等于参照view的对应的属性值,需要传递1个参数:(UIView)参照view1.4 & widthIs(100)方法名中带有“Is”的方法表示view的某一属性值等于参数数值,需要传递1个参数:(CGFloat)数值
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:9910次
排名:千里之外
原创:31篇
(1)(3)(6)(24)UITableViewCell自适应内容高度
其实TableViewCell自适应高度也就是Cell中的label自适应高度,网上有好多label自适应高度的方法,可以百度。
除了上面说的,还有一种比较简单的,其实原理估计都是一样的,只是个人觉得这种更好些而已。
首先我们知道返回cell自适应高度,那么这个高度怎么来,肯定是TableViewDelegate的方法-
(CGFloat)tableView:(UITableView
*)tableView
heightForRowAtIndexPath:(NSIndexPath
*)indexPath返回,那么具体的这么写呢,如下:
数据源方法该怎么写还怎么写,不用管(我的cell中IBOutlet了一个名为nameLabel的label,listArray是用来存储数据的数组)。
下面重点来了:
1,TestCell中申明一个方法:
2,TestCell中实现(-(void)cellAutoLayoutHeight:(NSString
*))这个方法:
注意事项:
1,nameLabel的lineNumbers要设置成0,因为我是 IBOutlet的
在xib中设置了0,所以代码里没写。
&2,TestCell必须相对cell本身的ContentView有约束(通过Editor
-& Pin -& 对应的约束,上下约束(即
top和bottom)一定要有),如果没有这个的话肯定也是不能实现的。
3,Cell中已经搞定了,下面看下这个方法:
每次新建cell时都会调用 heightForRowAtIndexPath:这个方法的,所以,在这里调用cell中的
cellAutoLayoutHeight:方法实时更新cell的高度,我们传进去的str就是用来判断多少字数然后是否对cell的高度进行增加操作用的。
********-----我是更新线-----********
看到评论说有没有纯代码的方法,其实纯代码和xib道理是一样的,都是在
heightForRowAtIndexPath方法里再次调用cellForRowAtIndexPath方法来确定高度,首先我们应该知道
UITableView委托方法执行的顺序,每次刷新tableView的时候先执行heightForRowAtIndexPath方法
确定cell的高度,然后再执行cellForRowAtIndexPath方法来给cell进行布局(换个方式解释,比如你叫我去商店帮你买东西,首先你要给我钱然然后我才能帮你买,你不给我钱我怎么买呢,这里的钱就是cell的高度,你不告诉tableView需要给cell多高它怎么创建布局,所以这个顺序必须明确),而我们把对cell的布局都是写在cell.m中或者在cellForRowAtIndexPath方法中的,所以只要在heightForRowAtIndexPath方法中调用cellForRowAtIndexPath方法就可以了,当然你可以在cell.m文件中定一个全局变量来存储cell的高度,当cell动态算完高度后把这个高度赋给你定义的全局变量,然后在heightForRowAtIndexPath方法中写入如下代码:
(CGFloat)tableView:(UITableView
*)tableView
heightForRowAtIndexPath:(NSIndexPath
*)indexPath{
//AskListTableViewCell是你自己定义的cell
&AskListTableViewCell
tableView:tableView
cellForRowAtIndexPath:indexPath];
//height是你自己在cell.m中定义的存储cell高度的全局变量
具体的cell如何布局就是你自己想怎么布局就怎么布局,动态计算label的高度可以用如下方法:
//动态计算label高度
)getHeighWithTitle:(NSString
font:(UIFont
width:(float)width
initWithFrame:CGRectMake(0,
& label.text
& label.font
& label.numberOfLines
& [label sizeToFit];
label.frame.size.height;
我把我写的一段代码贴出来吧,这样的话能更加清晰明了,可能我的代码布局部分会比较多:
//说明: “_”
打头的都是在cell.h文件中定义的全局变量,cellHeight就是全局的存储cell高度的变量,kScreenWith是屏幕的宽度,UIColorFromRGB是一个宏,用16进制颜色的宏,剩下的就是一些自己的布局了,content是内容,arr是评论和回复的数组,imgArray是存储图片的数组
-(void)initWithData:(NSString
andReplay:(NSArray
andImage:(NSArray
*)imgArray{
& _picImageView
[[UIImageView
initWithFrame:CGRectMake(15
& _picImageView.clipsToBounds
& _picImageView.layer.cornerRadius
& [_picImageView
setImage:[UIImage
imageNamed:@"wodepic_default"]];
& [_picImageView
setTag:2000];
addSubview:_picImageView];
& & //私信
& _siXinBtn
buttonWithType:UIButtonTypeCustom];
& [_siXinBtn
setSize:CGSizeMake(39
& [_siXinBtn
setCenter:CGPointMake(_picImageView.center.x,
_picImageView.frame.origin.y
_picImageView.frame.size.height
& [_siXinBtn
setBackgroundImage:[UIImage
imageNamed:@"sixinBtn"]
forState:UIControlStateNormal];
& [_siXinBtn
setTitle:@"私信"
forState:UIControlStateNormal];
& [_siXinBtn
setTitleColor:UIColorFromRGB(0x4f93b3)
forState:UIControlStateNormal];
& [_siXinBtn.titleLabel
setFont:[UIFont
systemFontOfSize:12.0f]];
& [_siXinBtn
setHidden:YES];
addSubview:_siXinBtn];
& & //姓名
& _nameLabel
initWithFrame:CGRectMake(_picImageView.frame.origin.x
_picImageView.frame.size.width
kScreenWith
(_picImageView.frame.origin.x
_picImageView.frame.size.width
& [_nameLabel
setFont:[UIFont
systemFontOfSize:15.0f]];
& [_nameLabel
setText:@"王小姐"];
& [_nameLabel
setTextColor:UIColorFromRGB(0x9b8325)];
& [_nameLabel
setTag:2002];
addSubview:_nameLabel];
& _cellHeigh
labelWidth =
kScreenWith
_nameLabel.frame.origin.x
& & //内容
& if(!_contentLabel){
_contentLabel
& [_contentLabel
setNumberOfLines:0];
& [_contentLabel
setLineBreakMode:NSLineBreakByWordWrapping];
& [_contentLabel
setFont:[UIFont
systemFontOfSize:15.0f]];
& [_contentLabel
setTextColor:UIColorFromRGB(0x333333)];
getHeighWithTitle:content
font:[UIFont
systemFontOfSize:15.0f]
width:labelWidth];
& [_contentLabel
setFrame:CGRectMake(_nameLabel.frame.origin.x
_nameLabel.frame.origin.y
_nameLabel.frame.size.height
labelWidth, heitht)];
& NSMutableAttributedString
attributedString1 = [[NSMutableAttributedString
initWithString:content];
& NSMutableParagraphStyle
paragraphStyle1 = [[NSMutableParagraphStyle
& [paragraphStyle1 setLineSpacing:6];
& [attributedString1 addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle1
range:NSMakeRange(0,
[content length])];
& [_contentLabel
setAttributedText:attributedString1];
& [_contentLabel
sizeToFit];
addSubview:_contentLabel];
& & //图片
& if([imgArray
imageArray
arrayWithArray:imgArray];
*imgbgView =
initWithFrame:CGRectMake(_contentLabel.frame.origin.x,
_contentLabel.frame.size.height
_contentLabel.frame.origin.y
_contentLabel.frame.size.width,
addSubview:imgbgView];
[imgArray count];
& & UIImageView
[[UIImageView
initWithFrame:CGRectMake(0
imgbgView.frame.size.height)];
& & [img setImageWithURL:[NSURL
URLWithString:[NSString
stringWithFormat:@"%@%@%@",HTTPURL,kURLUserPicture,[[imgArray
objectAtIndex:i]
objectForKey:@"id"]]]
placeholderImage:[UIImage
imageNamed:@"wodepic_default"]];
& & [imgbgView
addSubview:img];
& & UIButton
buttonWithType:UIButtonTypeCustom];
& & [btn setFrame:img.frame];
& & [btn setTag:(1000
& & [btn addTarget:self
action:@selector(clickImageViewAction:)
forControlEvents:UIControlEventTouchUpInside];
& & [imgbgView
addSubview:btn];
if(!_timeLabel){
& & _timeLabel
initWithFrame:CGRectMake(_nameLabel.frame.origin.x,
imgbgView.frame.origin.y
imgbgView.frame.size.height
& & [_timeLabel
setFrame:CGRectMake(_nameLabel.frame.origin.x,
imgbgView.frame.origin.y
imgbgView.frame.size.height
if(!_timeLabel){
& & _timeLabel
initWithFrame:CGRectMake(_nameLabel.frame.origin.x,
_contentLabel.frame.origin.y
_contentLabel.frame.size.height
& & [_timeLabel
setFrame:CGRectMake(_nameLabel.frame.origin.x,
_contentLabel.frame.origin.y
_contentLabel.frame.size.height
& [_timeLabel
setTextColor:UIColorFromRGB(0x999999)];
& [_timeLabel
setFont:[UIFont
systemFontOfSize:12.0f]];
& [_timeLabel
setText:@"
10:22:22"];
addSubview:_timeLabel];
& if(!_commentBtn){
_commentBtn
buttonWithType:UIButtonTypeCustom];
[_commentBtn
setSize:CGSizeMake(35,
[_commentBtn
setCenter:CGPointMake(kScreenWith
_timeLabel.center.y)];
[_commentBtn
setCenter:CGPointMake(kScreenWith
_timeLabel.center.y)];
& [_commentBtn
setImage:[UIImage
imageNamed:@"huifu"]
forState:UIControlStateNormal];
& [_commentBtn
setImageEdgeInsets:UIEdgeInsetsMake(6,
& [_commentBtn
setHidden:YES];
addSubview:_commentBtn];
& & //回复
UIImageView
[[UIImageView
initWithFrame:CGRectMake(_nameLabel.frame.origin.x
_commentBtn.frame.origin.y
_commentBtn.frame.size.height
[img setImage:[UIImage
imageNamed:@"hfbg"]];
addSubview:img];
CommentView
*commentView =
[[CommentView
[commentView getViewHeigh:arr
andWidth:labelWidth
[commentView setFrame:CGRectMake(_nameLabel.frame.origin.x,
img.frame.origin.y
img.frame.size.height,
labelWidth, commentView.viewHeigh)];
addSubview:commentView];
_cellHeigh
commentView.frame.origin.y
commentView.frame.size.height
_cellHeigh
_commentBtn.frame.origin.y
_commentBtn.frame.size.height
*lineView =
initWithFrame:CGRectMake(0,
_cellHeigh
kScreenWith,
& [lineView setBackgroundColor:UIColorFromRGB(0xeeeeee)];
addSubview:lineView];
//动态计算label高度
)getHeighWithTitle:(NSString
font:(UIFont
width:(float)width
initWithFrame:CGRectMake(0,
& label.text
& label.font
& label.numberOfLines
& [label sizeToFit];
label.frame.size.height;
以上布局效果图如下:
按照这样写之后,你会发现动态适应高度问题可以解决了,但是新的问题又来了,如果数据比较多的话,那么每次刷新tableView的时候
heightForRowAtIndexPath方法会调用一次cellForRowAtIndexPath方法,然后tableView自己也会调用一次cellForRowAtIndexPath方法,也就是说每次都会调用两次cellForRowAtIndexPath方法,而cellForRowAtIndexPath基本上都是布局的代码,这样会造成刷新tableView的时候卡顿现象的,数据少不会很明显但是数据比较多的情况下就一定会卡的,所以要给tableView进行优化,大家可以参考下我写的这篇博客,如果还不能满足的话那就是需要用到底层的优化了,具体的底层优化方案我暂时还不是很懂,如果有大神的话还请赐教,,,,
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。19:17 提问
IOS SWIFT UIControllerView 里的tableviewcell自适应高度
想实现让cell 根据label里的数据自动调整高度让label多行显示数据,根据网上给的自适应方法没有实现,还希望伙伴们给点指导,下面附上代码和截图
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.rowHeight = UITableViewAutomaticDimension
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath:NSIndexPath)-&CGFloat{
func numberOfSectionsInTableView(tableView: UITableView) -& Int
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -& Int
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -& UITableViewCell
var cell : TableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell
if(cell == nil)
cell = NSBundle.mainBundle().loadNibNamed("Cell", owner: self, options: nil)[0] as! TableViewC
cell.first.text = "nnfsfhsdsdsdifsfsfsdfisfsidfhsdfhdhfsdfsdfhdfhhfhsdfhsdhfshdhsdfsdd"
cell.second.text = "rirgjf"
cell.frame.size.height = cell.first.frame.size.height
return cell as UITableViewCell
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var first: UILabel!
@IBOutlet weak var second: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
按赞数排序
(CGSize)sizeForText:(NSString *)text Width:(CGFloat)width heigth:(CGFloat)heigth fontSize:(CGFloat)size{
CGRect rect = [text boundingRectWithSize:CGSizeMake(width,heigth) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:size]} context:nil];
return rect.
cell的自适应有更简单的方法,就是你写的那个,self.tableView.rowHeight = UITableViewAutomaticD
self.tableView.estimatedRowHeight = 2(数字随便给);还要给label设置约束
动态设定cell的高度,自己依据文本的内容,字体大小,指定宽度计算label的size,然后返回给tableView的cell高度的代理就可以。
楼上正解,他给的代码是正确的。顺便提一下,如果是在iOS6系统中运行,是没有boudingRectWithSize.....那个方法的,它只适用于iOS7及以上版本的系统。iOS6系统的话,此时很简单,调用sizeWithFont.....方法即可。不过,现在iOS9都出来了,不用适配iOS6了吧,太老了
其他相似问题}

我要回帖

更多关于 cell高度自适应 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信