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

来源:互联网 发布:网络本科统考 编辑:程序博客网 时间:2024/06/05 20:03

作者:朱克锋

邮箱:zhukefeng@iboxpay.com

转载请注明出处: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 {

    [superviewDidLoad];

    

    items = [[NSMutableArrayalloc] init];

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

    [itemsaddObject:@"your str  add by you"];

    [itemsaddObject:@"your str  add by you"];

    [itemsaddObject:@"your str  add by you"];

    [itemsaddObject:@"your str  add by you"];

    [itemsaddObject:@"your str  add by you"];

    [itemsaddObject:@"your str  add by you"];

    [itemsaddObject:@"your str  add by you"];

    [itemsaddObject:@"your str  add by you"];

    [itemsaddObject:@"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 [itemscount];

}


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

{

return1;

}


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

{

    NSString *text = [itemsobjectAtIndex:[indexPath row]];

    

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

    

    CGSize size = [textsizeWithFont:[UIFontsystemFontOfSize: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 = [[[UITableViewCellalloc] initWithFrame:CGRectZeroreuseIdentifier:@"MyCell"]autorelease];

        

        label = [[UILabelalloc] 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 = [itemsobjectAtIndex:[indexPath row]];

    

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

    

    CGSize size = [textsizeWithFont:[UIFontsystemFontOfSize:FONT_SIZE]constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    

    if (!label)

        label = (UILabel*)[cellviewWithTag: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


原创粉丝点击