iOS cell自動換行

來源:互聯網
上載者:User


//

//  DynamicHeightsViewController.h

//  DynamicHeights

//

//  Created by Matt Long on 9/22/09.

//  Copyright Skye Road Systems, Inc. 2009. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface DynamicHeightsViewController : UIViewController {

    IBOutlet UITableView *dataTableView;

    

    NSMutableArray *items;

}


@end




////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//

//  DynamicHeightsViewController.m

//  DynamicHeights

//

//  Created by Matt Long on 9/22/09.

//  Copyright Skye Road Systems, Inc. 2009. All rights reserved.

//


#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];

    [itemsaddObject:@"After two years in Washington, I often long for the realism and sincerity of Hollywood.\n\n\t\t-Fred Thompson, Speech before the Commonwealth Club of Californiaaaagdd阿黑哥是否規範的阿黑哥是否規範的阿黑哥是否規範的阿黑哥是否規範的阿黑哥是否規範的阿黑哥是否規範的阿黑哥是否ll"];

    [itemsaddObject:@"It is a profitable thing, if one is wise, to seem foolish.\n\n\t\t-Aeschylus (525 BC - 456 BC)"];

    [itemsaddObject:@"Bill Gates is a very rich man today... and do you want to know why? The answer is one word: versions.\n\n\t\t-Dave Barry"];

    [itemsaddObject:@"At the worst, a house unkept cannot be so distressing as a life unlived.\n\n\t\t-Dame Rose Macaulay (1881 - 1958)"];

    [itemsaddObject:@"It is curious that physical courage should be so common in the world and moral courage so rare.\n\n\t\t-Mark Twain (1835 - 1910)"];

    [itemsaddObject:@"The knowledge of the world is only to be acquired in the world, and not in a closet.\n\n\t\t-Lord Chesterfield (1694 - 1773), Letters to His Son, 1746, published 1774"];

    [itemsaddObject:@"What lies behind us and what lies before us are tiny matters compared to what lies within us.\n\n\t\t-Ralph Waldo Emerson (1803 - 1882), (attributed)"];

     

}


- (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;

{

return 1;

}


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

{

    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];

    

    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:@"Cell"];

    if (cell == nil)

    {

        cell = [[[UITableViewCellalloc] initWithFrame:CGRectZeroreuseIdentifier:@"Cell"]autorelease];

        

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

        [label setLineBreakMode:UILineBreakModeWordWrap];

        [label setMinimumFontSize:FONT_SIZE];

        [label setNumberOfLines:0];

        [label setFont:[UIFontsystemFontOfSize: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*)[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;


}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.