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

作者:朱克锋

邮箱:[email protected]

转载请注明出处:http://blog.csdn.net/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


你可能感兴趣的:(ios,测试,interface,layer)