纯代码TableView自适应高度(很老的使用方法)
2015-10-26 09:13
525 查看
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.numberOfLines = 100;
cell.textLabel.font = [UIFont systemFontOfSize:15];
cell.textLabel.text = self.arr[indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* text = self.arr[indexPath.row];
CGSize constraint = CGSizeMake(375-40, 100);
NSAttributedString* attributedText = [[NSAttributedString alloc]initWithString:text attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGSize size = rect.size;
CGFloat height = MAX(size.height, 20);
NSLog(@"%f",height);
return height+10;
}
{
return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.numberOfLines = 100;
cell.textLabel.font = [UIFont systemFontOfSize:15];
cell.textLabel.text = self.arr[indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* text = self.arr[indexPath.row];
CGSize constraint = CGSizeMake(375-40, 100);
NSAttributedString* attributedText = [[NSAttributedString alloc]initWithString:text attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
CGRect rect = [attributedText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGSize size = rect.size;
CGFloat height = MAX(size.height, 20);
NSLog(@"%f",height);
return height+10;
}
相关文章推荐
- C++ friend
- JAVA容器
- Hadoop启动报Error: JAVA_HOME is not set and could not be found解决办法
- C++学习笔录2
- Ubuntu下一个python的BeautifulSoup和rsa安装方法---信息检索project2部分:微博爬行要求python包裹
- java设计模式之适配器模式
- 2048游戏代码逻辑分析
- JDBC
- 从头认识java-4.4 this
- 从头认识java-4.4 this
- java IO流
- C++初始化列表
- 从头认识java-4.3 默认构造器
- Eclipse C/C++环境配置
- 从头认识java-4.3 默认构造器
- 【c++】必须在类初始化列表中初始化的几种情况
- Java解析XML格式数据的方法详解
- View代码布局
- java中sleep()和wait()的区别
- 构造干净的 Git 历史线索