- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 列寬
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何種字體進行顯示
UIFont *font = [UIFont systemFontOfSize:13];
// 該行要顯示的內容
NSString *content = [data objectAtIndex:indexPath.row];
// 計算出顯示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
// 這裏返回需要的高度
return size.height;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
// 列寬
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何種字體進行顯示
UIFont *font = [UIFont systemFontOfSize:13];
// 該行要顯示的內容
NSString *content = [data objectAtIndex:indexPath.row];
// 計算出顯示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
// 構建顯示行
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
// 設置顯示榘形大小
rect.size = size;
// 重置列文本區域
cell.textLabel.frame = rect;
cell.textLabel.text = content;
// 設置自動換行(重要)
cell.textLabel.numberOfLines = 0;
// 設置顯示字體(一定要和之前計算時使用字體一至)
cell.textLabel.font = font;
return cell;
}
分享到:
相关推荐
UITableviewCell高度自适应
UITableViewCell自适应高度,利用UITableView的UITableViewAutomaticDimension的特性,结合masonry实现纯代码cell自适应高度。
在开发中,uitableView 会经常用到,作为实现tableView展示数据的cell 也就显示出了非同一般的作用,经常,我们从网络上解析到数据之后,每条数据有多有少,这就要求cell有不同的高度,自适应高度的要求由此产生,...
使用SDAutoLayout 自适应UITableViewCell 自适应, Cell上需要展示三个属性右侧图片,titleLabel, textLabel, timeLabel. 四个属性, 需要自适应的label为 textLabel1, 白 糗事百科的数据接口封装在model中 然后添加在...
利用原始UITableViewcell强大的自适应性能,改造UITableViewcell,与自定义UITableViewcell略有不同
应用ios8新特性,ios8下tableviewcell内容自适应
只有一个UILabel,但是未设置Bottom的底部约束,导致UITableViewCell不能自适应高度.(默认高度为44) 场合4: 因为没有UILabel,所以UITableViewCell不能自适应高度.(默认高度为44) 环境: XCode7,Swift2.0 系统: IOS...
UITableViewCell高度的自适应
TableViewCellHightWithAutolayout 分别用了xib设置约束和手写约束代码来实现UItableViewCell的自适应高度
UITableviewCell高度自适应,SDAutolayout使用,微博,微信,图片查看,视频播放
UItableViewCell的自适应效果
最近在研究UITableViewCell高度的自适应,在网上找到了一个国内人员开发的工具类,自己下载下来研究了一下,感觉非常不错,用起来也非常方便,这是高手的博客地址...我自己也用着写了个demo,望大家参考一下。...
tableviewcell 输入内容实时刷新高度 自适应高度 有不足的地方请指教 分享一些互相学习
UITableViewCell的动态高度计算一直是一个交流讨论的话题,也已经有了很多实现的机制,但更多的是需要一些扩展类什么的,还有需要计算什么的,使用起来比较麻烦。因此这里推荐一种自动计算高度的方法,他是在我的...
UITableViewCell 动态高度计算 适用于ios8.0后 支持UITextView输入动态改变高度。
Cell嵌套UICollectionView自动布局获取高度示例
uitableviewcell 高度自适应代码demo,喜欢的可以看看,也可以一起讨论下,有什么更好的方法来解决这个问题。