最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
iOS8中实现UILabel的自适应高度的方法教程
时间:2015-07-21 编辑:简简单单 来源:一聚教程网
在ios应用开发的时候遇到如何使UILabel自适应高度,查了好多资料再通过自己手动,实现了这个效果,下面分享出来
相信大家都遇到这种情况,label没有根据text的字数来自适应高度
7、实现TableView的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法为cell赋值
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [tableData count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //指定cellIdentifier为自定义的cell static NSString *CellIdentifier = @"Cell"; //自定义cell类 TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[TableViewCell alloc] initWithReuseIdentifier:CellIdentifier]; } UserModel *user = [tableData objectAtIndex:indexPath.row]; cell.name.text = user.username; [cell.userImage setImage:[UIImage imageNamed:user.imagePath]]; [cell setIntroductionText:user.introduction]; return cell; }
8、最后需要将cell的高度返回给
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath方法: -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ TableViewCell *cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath]; return cell.frame.size.height; }
这样TableViewController.m中得所有代码:
// // TableViewController.m // AdaptiveCell // // Created by swinglife on 14-1-10. // Copyright (c) 2014年 swinglife. All rights reserved. // #import "TableViewController.h" #import "UserModel.h" #import "TableViewCell.h" @interface TableViewController (){ NSMutableArray *tableData; //tableView数据存放数组 } @end @implementation TableViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { tableData = [[NSMutableArray alloc] init]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; [self initTableView]; [self createUserData]; } //初始化tableView; -(void)initTableView{ CGRect frame = self.view.frame; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)]; //代理类 _tableView.delegate = self; //数据源 _tableView.dataSource = self; [self.view addSubview:_tableView]; } //我需要一点测试数据,直接复制老项目东西 -(void)createUserData{ UserModel *user = [[UserModel alloc] init]; [user setUsername:@"胖虎"]; [user setIntroduction:@"我是胖虎我怕谁!!我是胖虎我怕谁!!我是胖虎我怕谁!!"]; [user setImagePath:@"panghu.jpg"]; UserModel *user2 = [[UserModel alloc] init]; [user2 setUsername:@"多啦A梦"]; [user2 setIntroduction:@"我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!我是多啦A梦我有肚子!!"]; [user2 setImagePath:@"duolaameng.jpg"]; UserModel *user3 = [[UserModel alloc] init]; [user3 setUsername:@"大雄"]; [user3 setIntroduction:@"我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,我是大雄我谁都怕,"]; [user3 setImagePath:@"daxiong.jpg"]; [tableData addObject:user]; [tableData addObject:user2]; [tableData addObject:user3]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ TableViewCell *cell = [self tableView:_tableView cellForRowAtIndexPath:indexPath]; return cell.frame.size.height; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [tableData count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //指定cellIdentifier为自定义的cell static NSString *CellIdentifier = @"Cell"; //自定义cell类 TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[TableViewCell alloc] initWithReuseIdentifier:CellIdentifier]; } UserModel *user = [tableData objectAtIndex:indexPath.row]; cell.name.text = user.username; [cell.userImage setImage:[UIImage imageNamed:user.imagePath]]; [cell setIntroductionText:user.introduction]; return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
总结:这种方式是通过计算出UILabel自动换行后的高度后,通过-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 方法将高度返回给TableView然后构建cell的高度。
最后的运行效果:
相关文章
- sora软件价格介绍 02-22
- sora官网入口地址一览 02-22
- Sora生成的视频使用教程 02-22
- 《梦幻西游》元宵灯谜线索答案大全2024 02-22
- 《原神》有朋自远方来第一天通关攻略 02-22
- 《苏醒之路》四个结局达成攻略 02-22