IOS表视图动态高度实现实例

作者:朱克锋

邮箱:zhukefeng@iboxpay.com

转载请注明出处:http://blog.youkuaiyun.com/linux_zkf


文件头

#import <UIKit/UIKit.h>


@interface DynamicHeightsViewController : UIViewController {

    IBOutlet UITableView *dataTableView;

    

    NSMutableArray *items;

}


@end

文件实现

#import "DynamicHeightsViewController.h"


#define FONT_SIZE 14.0f

#define CELL_CONTENT_WIDTH 320.0f

#define CELL_CONTENT_MARGIN 10.0f


@implementation DynamicHeightsViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    items = [[NSMutableArray alloc] init];

    //your str  add by you 自己添加长度不同的字符进行测试

    [items addObject:@"your str  add by you"];

    [items addObject:@"your str  add by you"];

    [items addObject:@"your str  add by you"];

    [items addObject:@"your str  add by you"];

    [items addObject:@"your str  add by you"];

    [items addObject:@"your str  add by you"];

    [items addObject:@"your str  add by you"];

    [items addObject:@"your str  add by you"];

    [items addObject:@"your str  add by you"];

     

}


- (void)dealloc {

    [items release], items = nil;

    [super dealloc];

}


#pragma mark -

#pragma mark UITableView Delegaates


- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

return [items count];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

{

return 1;

}


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

{

    NSString *text = [items objectAtIndex:[indexPath row]];

    

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 99999.0f);

    

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    

    CGFloat height = MAX(size.height, 44.0f);

    

    return height + (CELL_CONTENT_MARGIN * 2);

}


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

{

    UITableViewCell *cell;

    UILabel *label = nil;

    

    cell = [tv dequeueReusableCellWithIdentifier:@"MyCell"];

    if (cell == nil)

    {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyCell"] autorelease];

        

        label = [[UILabel alloc] initWithFrame:CGRectZero];

        [label setLineBreakMode:UILineBreakModeWordWrap];

        [label setMinimumFontSize:FONT_SIZE];

        [label setNumberOfLines:0];

        [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];

        [label setTag:1];

        

        [[label layer] setBorderWidth:2.0f];

        

        [[cell contentView] addSubview:label];

        

    }

    NSString *text = [items objectAtIndex:[indexPath row]];

    

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    

    if (!label)

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

    

    [label setText:text];

    [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];

    

    return cell;


}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值