iOS UI10_cell自适应高度

#import "MainViewController.h"
#import "Cell.h"

@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)NSArray *picArr;
@property(nonatomic, retain)NSMutableArray *ziArr;
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor greenColor];
    self.navigationItem.title[email protected]"心静自然凉";

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    tableView.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:tableView];
//    [tableView setEditing:YES animated:YES];

    [tableView release];

    tableView.delegate = self;
    tableView.dataSource = self;
//    //他只会执行一次,可以设置行高,但是不灵活
//    tableView.rowHeight=100;

}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self create];
    }
    return self;
}

- (void)create
{
    self.picArr = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg",nil];
    self.ziArr = [NSMutableArray arrayWithObjects:@"中国共产党新闻网北京4月1日电 (万鹏)3月28日,习近平主席出席2015年博鳌论坛年会开幕式并发表了题为《迈向命运共同体 开创亚洲新未来》的主旨演讲,他强调,“亚洲是世界的亚洲。亚洲要迈向命运共同体、开创亚洲新未来,必须在世界前进的步伐中前进、在世界发展的潮流中发展。习主席的演讲传递了哪些重要信息?国务院参事室特邀研究员保育钧,中国国际问题研究院研究员杨希雨做客人民网时谈到,习主席主旨演讲展现出“五大亮点”,再次提出“亚洲方式”的新命题,开幕式本身可谓“一带一路”的各国大合唱,让人印象深刻", @"床前明月光,疑是地上霜.举头望明月,低头思故乡", @"NBA常规赛强强对话,勇士在一度落后17分的情况下,客场以110-106逆转快船,终结对手7连胜的同时豪取10连胜。库里全场轰下27分,并在第二节运球晃倒保罗,技惊四座。快船格里芬40分,外加12篮板5助攻",nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }

    cell.cellImageView.image=[UIImage imageNamed:self.picArr[indexPath.row]];
    cell.newLabel.text=self.ziArr[indexPath.row];

    return cell;
}

#pragma mark 这个方法是tableView的delegate所提供的协议方法,主要用来设置每一行的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    //根据图片的尺寸设置行高
    //
    UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]];
    //通过CGSize找到image里面图片的尺寸
    CGSize picSize=image.size;
    //计算行高
    CGFloat rowHeight = picSize.height * self.view.frame.size.width / picSize.width;

    //计算label高
    //根据对应的文字求出cell上的label显示的高度
    NSDictionary *fontDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    //根据文字的大小,计算出文本的尺寸
    //还需要执行一个尺寸(375,0)
    //第三个参数,计算高度需要根据字体的哪个特征来确定
    CGRect rect = [self.ziArr[indexPath.row]boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    //最后把结果作为返回值返回
    return rowHeight + rect.size.height;

}

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

@end
//
//  Cell.h
//  UI10_cell自适应高度
//
//  Created by dllo on 15/8/11.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface Cell : UITableViewCell
@property(nonatomic,retain)UIImageView *cellImageView;
@property(nonatomic,retain)UILabel *newLabel;

@end
//
//  Cell.m
//  UI10_cell自适应高度
//
//  Created by dllo on 15/8/11.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "Cell.h"

@implementation Cell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];

    }
    return self;
}

-(void)createView
{
    self.cellImageView=[[UIImageView alloc] init];
    self.cellImageView.backgroundColor=[UIColor yellowColor];
    [self.contentView addSubview:self.cellImageView];
    [_cellImageView release];

    self.newLabel=[[UILabel alloc] init];
    self.newLabel.backgroundColor=[UIColor yellowColor];
    //指定label的字体大小
    //默认字体是17
    self.newLabel.font=[UIFont systemFontOfSize:14];

    self.newLabel.numberOfLines=0;
    [self.newLabel sizeToFit];

    [self.contentView addSubview:self.newLabel];
    [_newLabel release];

}

-(void)dealloc
{
    [_cellImageView release];
    [_newLabel release];
    [super dealloc];
}

-(void)layoutSubviews
{

    [super layoutSubviews];
    //让imageView尺寸和图片大小相同
    //因为这个方法是最后一个执行的,所以执行到这个方法的时候,已经对cell各个属性完成赋值操作,所以可以通过imageView.image找到图片的尺寸
    CGSize picSize = self.cellImageView.image.size;
    CGFloat height = picSize.height * self.contentView.frame.size.width / picSize.width;
    self.cellImageView.frame =CGRectMake(0, 0, self.contentView.frame.size.width, height);

    NSDictionary *fontDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    //根据文字的大小,计算出文本的尺寸
    //还需要执行一个尺寸(375,0)
    //第三个参数,计算高度需要根据字体的哪个特征来确定
    CGRect rect = [self.newLabel.text boundingRectWithSize:CGSizeMake(375, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    self.newLabel.frame=CGRectMake(0, height, self.contentView
                                   .frame.size.width, rect.size.height);

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 21:03:08

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

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

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

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 UITextView自适应高度UITextContainerView抖动问题

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

【原】ios tableViewCell 自适应高度

原文:http://www.cnblogs.com/A--G/p/4819051.html 前言:之前在做一个类似微博的小需求时候,用table view实现了微博文字和图片等等的基本展示,由于文字和图片的数量问题,cell高度会受到影响. 以前的做法是在heightForRowAtIndexPath 里取出这条微博的model,计算图片和文字的高度,现在有一个简单一点的办法: 在heightForRowAtIndexPath里重新获取这个cell的高度即可, TableViewCell *ce

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 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.l

IOS UILabel 根据内容自适应高度

iOS Label 自适应高度  适配iOS7以后的版本 更多 self.contentLabelView = [[UILabel alloc] init]; self.contentLabelView.font = SYS_FONT(15); self.contentLabelView.lineBreakMode =NSLineBreakByTruncatingTail ; self.contentLabelView.textColor =  [UIColor colorWithHexStri