自定义UiTableViewCell高度

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

  2.   

  3.    static NSString *CellIdentifier = @"Cell"

  4.   

  5.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

  6.    if (cell == nil) { 

  7.         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

  8.         UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 

  9.         label.tag = 1; 

  10.         label.lineBreakMode = UILineBreakModeWordWrap; 

  11.         label.highlightedTextColor = [UIColor whiteColor]; 

  12.         label.numberOfLines = 0; 

  13.         label.opaque = NO;// 选中Opaque表示视图后面的任何内容都不应该绘制 

  14.         label.backgroundColor = [UIColor clearColor]; 

  15.         [cell.contentView addSubview:label]; 

  16.         [label release]; 

  17.     } 

  18.   

  19.     UILabel *label = (UILabel *)[cell viewWithTag:1]; 

  20.     NSString *text; 

  21.     text = [textArray objectAtIndex:indexPath.row]; 

  22.     CGRect cellFrame = [cell frame]; 

  23.     cellFrame.origin = CGPointMake(0, 0); 

  24.   

  25.     label.text = text; 

  26.     CGRect rect = CGRectInset(cellFrame, 2, 2); 

  27.     label.frame = rect; 

  28.     [label sizeToFit]; 

  29.    if (label.frame.size.height > 46) { 

  30.         cellFrame.size.height = 50 + label.frame.size.height - 46; 

  31.     } 

  32.    else

  33.         cellFrame.size.height = 50; 

  34.     } 

  35.     [cell setFrame:cellFrame]; 

  36.   

  37.    return cell; 

  38.  

  39. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

  40.     UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; 

  41.    return cell.frame.size.height; 

  42. 还有一种方法就是添加一个专门用于计算cell高度的类

  43. @interface DemoCell : NSObject{

       UILabel *_content;

    }

    -(CGFloat)contentHeight;

    -(void)setContent:(NSString *)content;

    @end;

    从上面的DemoCell来看其带有一个UILabel对象,这个Cell就是要根据UILabel的内容动态更改高度。其中contentHeight方法是返回Cell的高度。setContent是设置UILabel的内容并计算UILabel的高度。此类很简单要做的就是这样这些操作。然后我们在控制器中定义一个样本Cell专门用于计算Cell的高度。代码如下:

    #import "DemoCell.h"

    @interface DemoViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{

        UITableView *_tableView;

        DemoCell *_sampleCell;

        NSArray *_listData;

    }

    @end;

    其中部分实现代码如下:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        //在此使用样本Cell计算高度。

        NSString *content=[_listData objectAtIndex:indexPath.row];

        [_sampleCell setContent:content];

        return [_sampleCell contentHeight];

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        static NSString *cellId=@"DemoCell";

        DemoCell *cell=(DemoCell *)[tableView dequeueReusableCellWithIdentifier:cellId];

        if(cell==nil){

            cell=[[[DemoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];

        }

        [cell setContent:[_listData objectAtIndex indexPath:indexPath.row]];

        return cell;

    }

           到此为止我们已经顺利地展示了如何动态变更Cell的高度。只要内容变更我们调用UITableView的reloadData方法就可以刷新整个列表了。Cell如何计算高度本人没有列出来,因为不同的需求导致这部分的实现会不一样。这里只是把思路给大家交代清楚。具体的实现还是要各位童鞋们自己动手操作。

     

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值