iOS 根據文字內容設定cell 的高度
今天學習一個簡單的,根據內容的大小設定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
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.array = [NSMutableArray array];
NSMutableDictionary *dic = [NSMutableDictionarydictionaryWithObjectsAndKeys:@" 這天是許俊浩和沈睿言在一起交往三週年的日子.\n可是這麼美好的日子卻掀起了一場戰爭,沒錯,許俊浩覺得完全可以用戰爭來形容這場混亂。\n本來為了度過美好的今天,許俊浩還特地推掉了今天的工作安排。\n
[_array addObject:dic];
}
return self;
}
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorwhiteColor];
self.navigationController.navigationBar.translucent = NO;
[self create];
}
- (void) create{
UITableView *table = [[UITableViewalloc] initWithFrame:CGRectMake(0, 0, 375, 603) style:UITableViewStylePlain];
table.backgroundColor = [UIColorclearColor];
table.separatorColor = [UIColorblackColor];
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 = [[mainTableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:name];
}
NSDictionary *dic = [_array objectAtIndex:indexPath.row];
[cell setCustomDic:dic];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *strAtt = @{NSFontAttributeName:[UIFontsystemFontOfSize:17]};
NSDictionary *dic = [_array objectAtIndex:indexPath.row];
NSString *value = [dic objectForKey:@"name"];
CGFloat width = [UIScreenmainScreen].bounds.size.width;
CGRect strRect = [value boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOriginattributes:strAtt context:nil];
return strRect.size.height;
}
第四步:AppDelegate.m
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
_window.backgroundColor = [UIColorwhiteColor];
[_windowmakeKeyAndVisible];
[_window release];
mainViewController *main = [[mainViewControlleralloc] init];
UINavigationController *nav = [[UINavigationControlleralloc] initWithRootViewController:main];
UITabBarController *tab = [[UITabBarControlleralloc] init];
NSMutableArray *array = [NSMutableArrayarrayWithObject:nav];
tab.viewControllers = array;
[_windowsetRootViewController:tab];
[main release];
[nav release];
[tab release];