zoukankan      html  css  js  c++  java
  • iOS cell自动换行


     

    //

    //  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 = [[NSMutableArray alloc] init];

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

        [itemsaddObject:@"It is a profitable thing, if one is wise, to seem foolish. -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. -Dave Barry"];

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

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

        [itemsaddObject:@"The knowledge of the world is only to be acquired in the world, and not in a closet. -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. -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 [items count];

    }


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

    {

    return1;

    }


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

    {

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

        

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

        

        CGSize size = [text sizeWithFont:[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 = [items objectAtIndex:[indexPath row]];

        

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

        

        CGSize size = [text sizeWithFont:[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;


    }

  • 相关阅读:
    1063. Set Similarity
    A1047. Student List for Course
    A1039. Course List for Student
    最大公约数、素数、分数运算、超长整数计算总结
    A1024. Palindromic Number
    A1023. Have Fun with Numbers
    A1059. Prime Factors
    A1096. Consecutive Factors
    A1078. Hashing
    A1015. Reversible Primes
  • 原文地址:https://www.cnblogs.com/james1207/p/3341743.html
Copyright © 2011-2022 走看看