自适应高度

1.MyCell.h

#import <UIKit/UIKit.h>

@interface MyCell : UITableViewCell
@property(nonatomic,retain)UIImageView *picImageView;
@property(nonatomic,retain)UILabel *newsLabel;

@end

2.MyCell.m

@implementation MyCell

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

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

}

-(void)createView{
    self.picImageView=[[UIImageView alloc] init];
    self.picImageView.backgroundColor=[UIColor redColor];
    [self.contentView addSubview:self.picImageView];
    [self.picImageView release];
    
    self.newsLabel=[[UILabel alloc] init];
    [self.contentView addSubview:self.newsLabel];
    [_newsLabel release];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    //这个是cell显示之前走得最后一个方法
    UIImage *image = self.picImageView.image;
    
    CGFloat height=image.size.height*self.contentView.frame.size.width/image.size.width;
    self.picImageView.frame=CGRectMake(0, 0, self.contentView.frame.size.width, height);
    
    //计算label高度
    self.newsLabel.numberOfLines=0;
    self.newsLabel.font=[UIFont systemFontOfSize:14];
    NSString *str=self.newsLabel.text;
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];
    // 根据文本的内容和文本的字体进行计算高度
    // 参数1. 告诉系统, 文本显示的最大范围
    CGRect rect = [str boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width, 0) options:1<<0 attributes:dic context:nil];
    
    //对label进行尺寸设置
    self.newsLabel.frame=CGRectMake(0, height, self.contentView.frame.size.width, rect.size.height);
    
   
   
}


3.RootViewController.m

#import "RootViewController.h"
#import "MyCell.h"
/*和本题无关(按位左移)
typedef NS_ENUM(NSUInteger, NSMyEnum) {
    NSTestFirst=1<<0,
    NSTestSecond =1<<1,
    NSTestThird=1<<2,
    NSTestFouth=1<<3,
};
*/
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSArray *picArr;
@property(nonatomic,retain)NSMutableArray *ziArr;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor cyanColor];
    self.navigationController.navigationBar.translucent=NO;
    self.picArr=@[@"c2.jpg",@"c4.jpg",@"c6.jpg"];
    self.ziArr = [NSMutableArray arrayWithObjects:@"暗红色的看见爱上饭店的萨科技粉红色的二批佛卡吾普尔欧菲科技王鹏飞机哦哦我IE金佛微积分是快乐大解放路口撒的结合了苦涩监护人的空间分让人印象深刻", @"床前明月光,疑是地上霜.举头望明月,低头思故乡", @"NBA常规赛强强对话,勇士在一度落后17分的情况下,客场以110-106逆转快船,终结对手7连胜的同时豪取10连胜。库里全场轰下27分,并在第二节运球晃倒保罗,技惊四座。快船格里芬40分,外加12篮板5助攻",nil];
    
    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    NSLog(@"%ld",NSTestFirst|NSTestFouth);
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.picArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse=@"reuse";
    MyCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell=[[[MyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    cell.picImageView.image=[UIImage imageNamed:self.picArr[indexPath.row]];
    cell.newsLabel.text=self.ziArr[indexPath.row];
    return  cell;
}

#pragma mark 设置tableView每行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //在这个方法里,主要计算图片的尺寸,来设置tableView的行高
    UIImage *image=[UIImage imageNamed:self.picArr[indexPath.row]];
    //UIImage保存着实际图片的尺寸,而UIImageView是我们看见的图片的尺寸都通过它来设置
    
    //通过图片的实际尺寸和屏幕固定的宽进行等比例计算
    CGFloat rowHeight=self.view.frame.size.width * image.size.height /image.size.width;
    
    
    //计算文字的高度
    // 计算每一个字的大小
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];
    // 根据文本的内容和文本的字体进行计算高度
    // 参数1. 告诉系统, 文本显示的最大范围
    CGRect rect = [self.ziArr[indexPath.row]boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 0) options:1<<0 attributes:dic context:nil];
    // 然后把图片的高和文本的高为返回值返回
    return rowHeight + rect.size.height;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值