UITableViewCell自适应高度
2015-05-20 17:46
267 查看
在网上看见一个开源的扩展,UITableView+FDTemplateLayoutCell,让高度计算这个事情变的前所未有的简单。
UITableView 询问 cell 高度有两种方式。
1.针对所有 Cell 具有固定高度的情况
对于定高需求的表格,强烈建议使用这种(而非下面的)方式保证不必要的高度计算和调用。
2.实现 UITableViewDelegate 代理方法:
需要注意的是,实现了这个方法后,rowHeight 的设置将无效。所以,这个方法适用于具有多种 cell 高度的 UITableView。
这些方法做不到根据内容多少来自适应cell高度。
下面将介绍UITableView+FDTemplateLayoutCell如何实现cell自适应,使用极其简单
步骤:
自己建一个tableView控件,我这里使用了prototypeCells
demo代码如下:
唯一需要注意的地方就是cell重用的那个id必须是已经被注册过得,也就是我们前面prototypeCells里面的ID;
下面分享UITableView+FDTemplateLayoutCell的类文件:https://github.com/forkingdog/UITableView-FDTemplateLayoutCell
UITableView 询问 cell 高度有两种方式。
1.针对所有 Cell 具有固定高度的情况
2.实现 UITableViewDelegate 代理方法:
这些方法做不到根据内容多少来自适应cell高度。
下面将介绍UITableView+FDTemplateLayoutCell如何实现cell自适应,使用极其简单
步骤:
自己建一个tableView控件,我这里使用了prototypeCells
demo代码如下:
#import "ViewController.h" #import "DataCell.h" #import "UITableView+FDTemplateLayoutCell.h" @interface ViewController () { NSArray *array; } @property (nonatomic, assign) BOOL cellHeightCacheEnabled; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.mytableview.fd_debugLogEnabled = YES; array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"PropertyList.plist" ofType:nil]]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return [tableView fd_heightForCellWithIdentifier:@"hellocell" configuration:^(DataCell *cell) { cell.name.text = [[array objectAtIndex:indexPath.row] objectForKey:@"name"]; cell.content.text = [[array objectAtIndex:indexPath.row] objectForKey:@"content"]; }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return array.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSDictionary *dict = [array objectAtIndex:indexPath.row]; DataCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hellocell" forIndexPath:indexPath]; cell.name.text = [dict objectForKey:@"name"]; cell.content.text = [dict objectForKey:@"content"]; return cell; } @end
唯一需要注意的地方就是cell重用的那个id必须是已经被注册过得,也就是我们前面prototypeCells里面的ID;
下面分享UITableView+FDTemplateLayoutCell的类文件:https://github.com/forkingdog/UITableView-FDTemplateLayoutCell
相关文章推荐
- UITableViewCell的高度与UILabel自适应
- UIlabel自适应高度和UITableViewCell自适应高度
- UITableViewCell高度自适应探索--AutoLayout结合Frame
- 实现自适应高度的 UITableViewCell 或 UILabel
- UITableViewCell 高度自适应
- 实现自适应高度的 UITableViewCell 或 UILabel
- ios8 Autolayout实现uitableviewcell 高度自适应
- 实现自适应高度的 UITableViewCell 或 UILabel
- 实现自适应高度的 UITableViewCell 或 UILabel
- 关于UITableViewCell的自适应高度使用后的心得记录
- UITableViewCell自适应高度
- UITableViewCell自适应高度
- Label的高度自适应和UItableViewCell的高度自适应
- UITableViewCell根据内容自适应高度
- UILabel自适应调试 UITableViewCell自适应高度
- UITableViewCell 高度自适应
- 实现自适应高度的 UITableViewCell 或 UILabel
- 32. 自定义UITableViewCell(高度自适应)
- uitableviewcell高度自适应笔记
- UITableViewCell自适应高度