zoukankan      html  css  js  c++  java
  • IOS XIB Cell自适应高度实现

    1.代码实现Cell高度自适应的方法

       通过代码来实现,需要计算每个控件的高度,之后获取一个cell的

    总高度,比较常见的是通过lable的文本计算需要的高度。

    CGSize labelsize = [@"asdassdas" sizeWithFont:font constrainedToSize:CGSizeMake(320,2000) lineBreakMode:NSLineBreakModeWordWrap];

     这样就可以计算展示需要的高度,cell里面展示的时候可以在代理的方法内放回高度就行了。今天要实现的

    是通过auto layout实现获取展示需要的高度,实现的具体是使用了一个第三方库(https://github.com/forkingdog/UITableView-FDTemplateLayoutCell),简化了实现,使用这个

    库可以让你专注的设置约束,下载后把UITableView+FDTemplateLayoutCell.h,UITableView+FDTemplateLayoutCell.m拖入项目就好,也可以通过pod 安装。

    2.实现效果

             

    3.实现代码

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [arrData count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        MyTableViewCell *cell = [self.tabview dequeueReusableCellWithIdentifier:@"CTF"];
        cell.txt.text = [arrData objectAtIndex:indexPath.row];
        cell.img.image=[UIImage imageNamed:@"a.jpg"];
        return cell;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return [tableView fd_heightForCellWithIdentifier:@"CTF" configuration:^(MyTableViewCell *cell) {
            cell.txt.text = [arrData objectAtIndex:indexPath.row];
            cell.img.image=[UIImage imageNamed:@"a.jpg"];
        }];
    }

       上传比较麻烦, 需要Demo的可以留邮箱,我会及时发。

  • 相关阅读:
    CodeForces-786B Legacy (线段树优化建图,单源最短路)
    CodeForces-528C Data Center Drama
    CodeForces-723E One-Way Reform
    2-SAT入门
    POJ-3683 Priest John's Busiest Day (2-SAT 求任意可行方案)
    转载: 8天学通MongoDB——第一天 基础入门
    C# 非EF注册登录与EF注册登录
    Asp.Net入门(三)
    非EF分页
    sql语句错误大集合
  • 原文地址:https://www.cnblogs.com/try-wyh/p/4748648.html
Copyright © 2011-2022 走看看