iOS动态改变TableView Cell高度
我们知道tableview的heightForRowAtIndexPath 会在 cellForRowAtIndexPath 方法之前执行,因此在计算cell高度的时候就不能通过表格的cell来计算,这样就导致动态计算高度变得有点困难。今天在网上找到下面的一种方法:
创建表格的cell
#pragma mark 表格-创建cell -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString* identifier = @"basis-cell"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell==nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } NSInteger section = indexPath.section; NSInteger width = [Tool loadScreenSize].width; NSInteger height =100+50*section-10; UIView* view = [[UIView alloc]initWithFrame:CGRectMake(0+1, 0+1, width-2, height)]; view.backgroundColor=[UIColor grayColor]; UIButton* btn = [Tool createButton:CGRectMake(5, 5, 100, 40) withDelegate:self withAction:nil withTitle:@"button" withBgColor:[UIColor yellowColor]]; [view addSubview:btn]; [cell.contentView addSubview:view]; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [[heights_ objectAtIndex:indexPath.section] setObject:[NSString stringWithFormat:@"%f",view.frame.size.height] atIndex:indexPath.row]; return cell; }
表格高度设置,在方法heightForRowAtIndexPath重写中先调用下方法:UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; 先把表格的cell手动生成出来,然后获取其高度。
#pragma mark 表格-配置cell高度 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; UIView* view = [cell.contentView.subviews objectAtIndex:0]; NSLog(@"cell.frame.size.height=%f",view.frame.size.height); return view.frame.size.height; }
相关推荐
tableview手动适应cell高度,以及iOS8之后自动适应cell高度
swift ,使用autolayout + storyboard 最外层tableview列表,cell里面嵌套了tableview,自适应内容,cell里的tableview不可滑动,内容全部显示,且文字分行显示,不用设置cell的高度直接可以自适应内容
IOS开发 tableview中cell的用法 值得拥有
tableview cell 倒计时
WKWebView调用js 代码自适应cell高度 github下载地址:https://github.com/yanhaiqiang/HQWebViewFit.git
巧妙运用 自动布局动态计算cell 高度
根据每个cell上数据的多少不同,cell占有不同的高度,一个简单的demo
Github最新版本:https://github.com/netyouli/WHC_AutoLayoutKit 你们的支持是我前进的动力
点击cell上面的按钮进行当前cell的展开和收缩,并只进行当前行的刷新。
GIF图过大不能上传,大家到博客里看吧:http://www.henishuo.com/ios-cell-embed-tableview/ 最好到GITHUB下载最新代码,博客文章末尾有下载地址。
tableviewcell 输入内容实时刷新高度 自适应高度 有不足的地方请指教 分享一些互相学习
swift tableview cell展开/折叠 填写信息, tableview自适应高度,tableview的cell嵌套tableview 自适应
实现:可以分组对应tableView的section,点击section展开和收回cell。 创建一个临时数组selectedArr存储需要展开的section。点击section是判断selectedArr是否包含该组,如果包含则移除,不包含则添加到selectedArr...
TableView的Cell上播放视频
感谢博主的http://blog.csdn.net/woaifen3344/article/details/50779743博客,有灵感去模仿和改造做出目前类似微博或者朋友圈的界面,实现了评论的添加和删除,动态计算高度和缓存。里面用到的技术有masonry,...
swift tableview 每个cell点击展开点击折叠,展开的时候可以填写信息(textfield),刷新tableview不受影响,实时获取到cell上填写的信息数据
swift 3 tableview cell 自适应文字展开动画
只需要3部设置就可以实现多cell的tableview高度自适应。 用到的库:https://github.com/gsdios/SDAutoLayout 视频教程:http://www.letv.com/ptv/vplay/24038772.html 新建自动布局交流QQ群:497140713