今天学习一个简单的,根据内容的大小设置cell 的高度.
第一步:建两个类,分别继承于UIViewController 和UITableViewCell .
第二步:
mainViewController.h
#import <UIKit/UIKit.h>
@interface mainViewController : UIViewController
@property (retain, nonatomic) NSMutableArray *array;
第三步:
mainViewScroller.m
#import "mainViewController.h"
#import "mainTableViewCell.h"
@interface mainViewController ()<UITableViewDataSource, UITableViewDelegate>
@end
@implementation mainViewController
- (void)dealloc{
[self.array release];
[super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.array = [NSMutableArray array];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@" 这天是许俊浩和沈睿言在一起交往三周年的日子.\n可是这么美好的日子却掀起了一场战争,没错,许俊浩觉得完全可以用战争来形容这场混乱。\n本来为了度过美好的今天,许俊浩还特地推掉了今天的工作安排。\n岂料因为不满沈睿言沉浸在网络游戏里,许俊浩一时冲动拔掉了网线,所以就这样惹怒了他的小情人沈睿言。\n“你这个混蛋!你怎么可以在我交易物品的时候拔掉我的网线?!”沈睿言怒气冲冲一把揪住许俊浩的衣领咆哮着。\n 被沈睿言这么一吼,许俊浩的火气也上来了,他一把甩开沈睿言揪着自己衣领的手,骂道:“天天就知道游戏!就算你是职业玩家也用不着这么拼命吧?!”\n“不这样哪有钱!”沈睿言马上愤怒的反驳。\n“我难道养不起你吗?!”许俊浩说了这么一句之后,准备动手把那些电脑都砸了,“这些电脑是你对象吗?!妈的,有没有搞清楚你到底在跟谁交往?!”\n沈睿言被他这样骂,再加上看到他要砸电脑的动作,便立刻动手用力推开了许俊浩。",@"name", nil];
[_array addObject:dic];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.translucent = NO;
[self create];
}
- (void) create{
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 603) style:UITableViewStylePlain];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = [UIColor blackColor];
table.dataSource = self;
table.delegate = self;
[self.view addSubview:table];
[table release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *name = @"ONE";
mainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:name];
if (!cell) {
cell = [[mainTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:name];
}
NSDictionary *dic = [_array objectAtIndex:indexPath.row];
[cell setCustomDic:dic];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *strAtt = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
NSDictionary *dic = [_array objectAtIndex:indexPath.row];
NSString *value = [dic objectForKey:@"name"];
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGRect strRect = [value boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:strAtt context:nil];
return strRect.size.height;
}
AppDelegate.m
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_window.backgroundColor = [UIColor whiteColor];
[_window makeKeyAndVisible];
[_window release];
mainViewController *main = [[mainViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:main];
UITabBarController *tab = [[UITabBarController alloc] init];
NSMutableArray *array = [NSMutableArray arrayWithObject:nav];
tab.viewControllers = array;
[_window setRootViewController:tab];
[main release];
[nav release];
[tab release];