备忘-给UITabelView内的单元格分别定制高度

主要有两行代码,分别写在UITableView的两个代理方法里面:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath(显示单元格的内容)

- (float )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath(定制单元高度-所有单元格的高度都一样)


参考代码如下:

[代码]c#/cpp/oc代码:

01 设置单元格内容的代理方法
02 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
03 {
04     //
05     static NSString *cellStr = @"cell";
06     //实例一个myCell类的对象cell。
07     //
08     CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
09     //
10     if (cell == nil)
11     {
12         cell = [[CommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
13     }
14      
15     //获得当前评论内容的高度,赋值给cell,之后定制单元格高度的时候我们只要用到cell的高,其它参数设为0对单元格的定制是不会有影响的
16     [cell setFrame:CGRectMake(0, 0, 0, self.cellAddHight)];
17  
18     return cell;
19 }

[代码]c#/cpp/oc代码:

1 - (float )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3     // 通过indexPath确定定制高度的是第几行
4     UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
5     // 返回自适应高度
6     return 99+cell.frame.size.height;
7 }

以上的代码是做了注释的是主要代码,有了这三行,再结合我之前写的UILabe自适应高度就能写出如下的定制UITableView效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值