//

//  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.\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 [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;

}

iOS cell自动换行的更多相关文章

  1. ios cell左滑删除

    iOS项目开发小技能 (三) -UITableView实现Cell左划删除等自定义功能 www.MyException.Cn  网友分享于:2015-06-05  浏览:0次   iOS项目开发小技巧 ...

  2. iOS Cell异步图片加载优化,缓存机制详解

    最近研究了一下UITbleView中异步加载网络图片的问题,iOS应用经常会看到这种界面.一个tableView上显示一些标题.详情等内容,在加上一张图片.这里说一下这种思路. 为了防止图片多次下载, ...

  3. ios cell展示可滑动的图片

    需求: 点击cell上的图片.图片以原图显示出来,可以放大或缩小.再次点击图片移除图片显示原来界面.(和QQ空间看图片类似) 点击图片实现效果: 1. 自定义一个 UITableView (KDIma ...

  4. ios cell常用属性

    1.设置UITableViewCell的accessoryView 有时候我们需要设置cell的一些样式,比如下图, 这个就是设置了cell的accessory属性的内容,如果我们想在上面显示Swit ...

  5. ios -- cell的图片下载

    1.面试题 1> 如何防止一个url对应的图片重复下载 * “cell下载图片思路 – 有沙盒缓存” 2> SDWebImage的默认缓存时长是多少? * 1个星期 3> SDWeb ...

  6. IOS Label 自动换行 IOS6和IOS7

    IOS 6和ios7 不一样,所以,我们分开来: IOS6: //计算实际frame大小,并将label的frame变成实际大小     CGSize size01 = [ssizeWithFont: ...

  7. IOS Cell重用机制

    重用机制: -(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *) ...

  8. iOS cell左滑出现多个功能按钮(IOS8以后支持)

    #import "ViewController.h" #import "Swift_OC-Swift.h" @interface ViewController ...

  9. ios cell时间相同隐藏算法

随机推荐

  1. systrace跟踪 Android性能优化

    http://blog.csdn.net/oujunli/article/details/8138172 http://blog.csdn.net/oujunli/article/details/50 ...

  2. ecshop了解01

    ecshop 是一个基于b2c的开源商城系统,从现在起来我也来学习一下,它是基于面向对象的,当然里面也有类. ecshop 的目录介绍 上面简单介绍一个ecshop的几个主要的文件,上面已经截图给大家 ...

  3. TextView使用Spannable设置复合文本

    http://blog.csdn.net/linghu_java/article/details/32053167 Spannable 对文字的编辑减少TextView的拼接并且达到改变一串字符中的部 ...

  4. angularjs kindEditor 中自定义按钮 弹出dialog

    1.angular-kindeditor.js 第38行左右加 editorConfig.items = ["placehoder"]; 2.en.js 第234行 placeho ...

  5. [转帖]FPGA--Vivado

    来源:http://home.eeworld.com.cn/my/space-uid-639749-blogid-267593.html 一般的,在Verilog中最常用的编码方式有二进制编码(Bin ...

  6. SGU 223.Little Kings

    时间限制:0.25s 空间限制:4M 题意: 在 n*n(n≤10)的棋盘上放 k (k<=n*n)个国王(可攻击相邻的 8 个格子),求使它们无法互相攻击的方案数. Solution: 采用状 ...

  7. gvim 常用命令

    插入: insert 强退: :q! 退出: :q 保存: :w 保存退出::wq 复制: yy(单行)   多行:8yy 删除: dd(单行)   多行:8dd 或者 :4,8d 执行脚本: :! ...

  8. TortoiseGit(乌龟git)保存用户名密码的方法(转)

    转自:http://my.oschina.net/jjyuangu/blog/232798?p=1 windows下比较比较好用的git客户端有2种: 1. msysgit + TortoiseGit ...

  9. Java学习----到底调用哪一个方法(多态)

    public class Father { public void print() { System.out.println("Father:print()"); } } publ ...

  10. Jquery异步请求数据实例

    一.Jquery向aspx页面请求数据 前台页面JS代码: $("#Button1").bind("click", function () { $.ajax({ ...