iOS tableview动态高度
来源:互联网 发布:淘宝和易趣的共同点 编辑:程序博客网 时间:2024/05/02 01:19
#import <UIKit/UIKit.h>
@interface DynamicHeightsViewController : UIViewController {
IBOutlet UITableView *dataTableView;
NSMutableArray *items;
}
@end
#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];
[items addObject:@"Happiness is having a large, loving, caring, close-knit family in another city.\n\n\t\t-George Burns (1896 - 1996)"];
[items addObject:@"When I am abroad, I always make it a rule never to criticize or attack the government of my own country. I make up for lost time when I come home.\n\n\t\t-Sir Winston Churchill (1874 - 1965)"];
[items addObject:@"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 California"];
[items addObject:@"It is a profitable thing, if one is wise, to seem foolish.\n\n\t\t-Aeschylus (525 BC - 456 BC)"];
[items addObject:@"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"];
[items addObject:@"At the worst, a house unkept cannot be so distressing as a life unlived.\n\n\t\t-Dame Rose Macaulay (1881 - 1958)"];
[items addObject:@"It is curious "];
[items addObject:@"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"];
[items addObject:@"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;
{
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), 20000.0f);
NSDictionary * attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:FONT_SIZE] forKey:NSFontAttributeName];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:text
attributes:attributes];
CGRect rect = [attributedText boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2) + 15;
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectMake(15, 28, 0, 0)];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setMinimumScaleFactor: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);
NSAttributedString *attributedText = [[NSAttributedString alloc]initWithString:text attributes:@{
NSFontAttributeName:[UIFont systemFontOfSize:FONT_SIZE]
}];
CGRect rect = [attributedText boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
NSLog(@"ss%@",label.text);
[label setFrame:CGRectMake(15, 28, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
[[cell contentView] addSubview:label];
return cell;
}
@end
0 0
- iOS tableview动态高度
- iOS 设置tableview动态高度
- IOS tableView cell动态高度 (autoLayout)
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- Swift iOS tableView static cell动态计算高度
- IOS tableView自适应高度
- iOS tableview自适应高度
- ios TableView tableHeader高度不正确
- ios开发笔记之如何让tableView根据文本内容动态改变cell的高度
- tableview动态算一次高度的方法
- 动态改变tableview的cell的高度
- MVVM动态计算Cell套TableView高度
- 动态设置TableView加载完毕后TableView的高度
- iOS 改变TableHeadView的高度,tableView自适应
- ios tableview 自动计算cell高度
- [iOS]TableView的Cell高度自适应
- 个人学习计划
- jQuery事件之鼠标事件
- jquery 提示框
- 系统上线小结
- 行链接和行迁移的秘密
- iOS tableview动态高度
- 很好的setuid资料整理----转载
- 关于母爱感人的文章
- 安卓应用基础学习
- Git分支管理策略
- 使用ext封装自定义组件-导航栏组件
- 华师大致学教育
- Win32 Windows编程 十一
- git add