cell自适应高度

#import "ViewController.h"
//屏幕宽和高
#define SCREEN_WIDTH    ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT   ([UIScreen mainScreen].bounds.size.height)

#define bgTableView [UIColor colorWithRed:235.0f/255.0f green:235.0f/255.0f blue:233.0f/255.0 alpha:1]

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *MyTableView;
    CGFloat heightCell;
    NSMutableArray *dataArray;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"cell高度自适应";
    
    dataArray=[[NSMutableArray alloc]initWithObjects:@"霜雪千年",@"梨花香",@"缠着衣角掠过熙攘,复悄入红帘深帐,听枝头黄鹂逗趣儿",@"细风绕指淌,坐船舫,兰桨拨开雾霭迷茫,不觉已一日过半,过眼的葱郁风光,悉数泛了黄,褪尽温度的风,无言牵引中,便清晰了在此的眉目,暮色的消融,隐约了晦朔葱茏,在这老街回眸,烟云中追溯我是谁,只消暮雨点滴",@"便足以粉饰这是非,待这月色涌起,谁人轻叩这门扉",@"苔绿青石板街,斑驳了流水般岁月,小酌三盏两杯,理不清缠绕的情结,在你淡漠眉间,瞥见离人的喜悲霜雪", nil];
    
    MyTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:MyTableView];
    MyTableView.backgroundColor=bgTableView;
    [MyTableView setDataSource:self];
    [MyTableView setDelegate:self];
    //允许TableView滑动
    MyTableView.scrollEnabled=YES;
    
    MyTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [MyTableView release];

}

#pragma mark-UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyTableView.separatorStyle=UITableViewCellSeparatorStyleNone;//取消系统的分割线
    static NSString *CellIdentifier = @"Cell";
    // 通过indexPath创建cell实例 每一个cell都是单独的
    UITableViewCell*cell = [MyTableView cellForRowAtIndexPath:indexPath];
    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    UILabel *materialNameLbe=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH-10*2, 20)];
    [cell addSubview:materialNameLbe];
   materialNameLbe.textAlignment=NSTextAlignmentLeft;
    materialNameLbe.font=[UIFont systemFontOfSize:16];
    
    materialNameLbe.text=dataArray[indexPath.row];
    materialNameLbe.numberOfLines=0;
    materialNameLbe.lineBreakMode=NSLineBreakByWordWrapping;
    CGSize size=[materialNameLbe sizeThatFits:CGSizeMake(materialNameLbe.frame.size.width, MAXFLOAT)];
    
    UIView *xianView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    [cell addSubview:xianView];
    xianView.backgroundColor=bgTableView;
    
    if (size.height>20) {
        materialNameLbe.frame=CGRectMake(10, 5, materialNameLbe.frame.size.width, size.height);
        
        heightCell=size.height+5*2;
        
        xianView.frame=CGRectMake(0, size.height+5*2, SCREEN_WIDTH, 1);
        
    } else {
        
        materialNameLbe.frame=CGRectMake(10,5, materialNameLbe.frame.size.width, 20);
        
        heightCell=20+5*2;
        
        xianView.frame=CGRectMake(0, 20+5*2, SCREEN_WIDTH, 1);
        
    }
    
    [materialNameLbe release];
    [xianView release];
    
    return cell;
}

#pragma mark-UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UILabel *materialNameLbe=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH-10*2, 20)];
    materialNameLbe.textAlignment=NSTextAlignmentLeft;
    materialNameLbe.font=[UIFont systemFontOfSize:16];
    
    materialNameLbe.text=dataArray[indexPath.row];

    materialNameLbe.numberOfLines=0;
    materialNameLbe.lineBreakMode=NSLineBreakByWordWrapping;
    CGSize size=[materialNameLbe sizeThatFits:CGSizeMake(materialNameLbe.frame.size.width, MAXFLOAT)];
    if (size.height>20) {
        materialNameLbe.frame=CGRectMake(10, 5, materialNameLbe.frame.size.width, size.height);
        heightCell=size.height+5*2;
        
        
    } else {
        materialNameLbe.frame=CGRectMake(10,5, materialNameLbe.frame.size.width, 20);
        
        heightCell=20+5*2;
        
    }
    
    [materialNameLbe release];
    
    return heightCell+1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 ElementUI 的表格组件,如果你想要实现单元格高度自适应,可以尝试以下方法: 1. 使用 slot-scope 自定义单元格内容: 在表格组件中,可以使用 slot-scope 来自定义单元格内容。你可以在 slot-scope 中访问到每个单元格的数据,然后根据数据的长度或内容来动态设置单元格的高度。例如: ```html <el-table :data="tableData"> <el-table-column prop="name" label="姓名"> <template slot-scope="scope"> <div :style="{ height: scope.row.name.length > 10 ? 'auto' : '30px' }"> {{ scope.row.name }} </div> </template> </el-table-column> <!-- 其他列 --> </el-table> ``` 在上述示例中,我们通过判断 `scope.row.name` 的长度来决定是否设置 `div` 的高度为 `auto`,如果长度超过 10 就设置为 `auto`,否则设置为固定的 `30px`。 2. 使用 CSS 样式控制单元格高度: 你也可以使用 CSS 来控制单元格的高度。使用 `el-table-column` 的 `className` 属性来添加自定义样式类,然后在这个样式类中设置单元格的高度。例如: ```html <style> .custom-cell { height: 30px; } </style> <el-table :data="tableData"> <el-table-column prop="name" label="姓名" :class-name="'custom-cell'"></el-table-column> <!-- 其他列 --> </el-table> ``` 在上述示例中,我们在 `el-table-column` 中使用 `:class-name` 属性来绑定样式类 `custom-cell`,然后在样式中设置了单元格的高度为 `30px`。 以上是两种常用的方法来实现 ElementUI 表格单元格高度自适应。你可以根据实际需求选择合适的方法进行使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值