打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS动态改变TableView Cell高度
在做tableView的时候,我们有时候需要根据model里面的某个成员变量是否存在来动态调整cell的高度,最近在网上看到一段代码不错,跟大家Share一下。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
类中获取cell的高度:
[objc] view plaincopy
CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX);
cell.textLabel.text = @"12345678900123456789";
cell.userInteractionEnabled = NO;
cell.textLabel.numberOfLines = 0;
CGSize requiredSize = [cell.textLabel.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];
CGRect rect = cell.frame;
rect.size.height = requiredSize.height+5;
cell.frame = rect;
这时候获取到了cell的高度,然后在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
类中改变cell的高度:
[objc] view plaincopy
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
NSLog(@"cell height %f",cell.frame.size.height);
return cell.frame.size.height;
这样以来cell的高度自动改变啦。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
IOS 如何动态设置每个TableViewCell的高度?
iOS8 UITableView动态加载cell的高度
使用estimatedRowHeight的优缺点
ios tableView那些事 (五) 给tableview设置缩进级别
iOS 中 TableView 内嵌套 CollectionView 动态高度的实现
详解Autolayout 设置UITableViewCell 高度问题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服