iOS界面ios开发记录

iOS开发之UITableView+FDTemplateLayo

2017-04-19  本文已影响2515人  朱晓晓的技术博客

1.导入文件

Paste_Image.png

2.在viewDidLoad打开开关和注册

Paste_Image.png
- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self AccessNetworkForDateMethod];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.ZTableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.ZTableView.delegate = self;
    self.ZTableView.dataSource =self;
    [self.view addSubview:self.ZTableView];
    
#warning  log日志是否输出
    self.ZTableView.fd_debugLogEnabled = YES;
    
    
#warning 注册Cell类,无论是纯代码的Cell,还是Xib的cell,否则会报错
    [self.ZTableView registerClass:[ZTableViewCell class] forCellReuseIdentifier:@"myCell"];

}

3.在返回高度方法中预缓存高度值

Paste_Image.png

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
#warning 预缓存高度
    return [self.ZTableView fd_heightForCellWithIdentifier:@"myCell" cacheByKey:indexPath configuration:^(ZTableViewCell *cell) {
        
        cell.cellModel = self.listArry[indexPath.row];
        
    }];
    
    
   // [self.ZTableView fd_heightForCellWithIdentifier:@"myCell" cacheByKey:indexPath configuration:^(id cell) {
        
//    }];
}


4.自定义Cell类注意

Paste_Image.png
-(void)initCell{

    self.sumLab = [[UILabel alloc]init];
    
#warning 所有的控件都是放置在"self.contentView"
    [self.contentView addSubview:self.sumLab];
    
    self.sumLab.text =@"测试阶段";
#warning Lable行数限制设为0
    self.sumLab.numberOfLines = 0;
    
#warning 使用Masonry设置高度大小位置,无论如何,基于Bottom这个点是不能忽略的,位置也都是基于self.contentView
    [self.sumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.contentView.mas_top).offset(5);
        make.left.mas_equalTo(self.contentView.mas_left).offset(5);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-5);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-5);

    }];

}

运行效果如下:


非代码挖坑注意:

如果发现代码运行在不同Xcode版本上,发现高度无法自适应,请替换最新的UITableView+FDTemplateLayoutCell框架,就能修正此问题.

demo下载:

https://github.com/OwenJoe/cellHeight.git

挖坑过程碰到很多问题,参阅文章如下:
http://blog.sunnyxx.com/2015/05/17/cell-height-calculation/
http://www.jianshu.com/p/385e0bfebba2
http://www.jianshu.com/p/ca11e0e07de4

上一篇 下一篇

猜你喜欢

热点阅读