just explain it with code:
// ViewController.m
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_items = [[NSMutableArray alloc] initWithObjects:@"one", @"tow fjdsl fjdslf slfjls flsjflsjf lsjflsjfls jflsjflsjf lsjf lsjflsj flsjflsjflsjf lsfj lsfjslfj ltwo", @"three but so what r fdlsf slf slf jslfj slfj lsf jlsjf lsfjlsfjiosj flsjf lsjf ;sfj s;jf s;jfs jfs; fjs;fj fs ;fslflsflsf lflsflsflsfslolling in the deep, we could have it all!", @"four", @"five", @"six", @"sefjd slfjslf jlsjflsjflsjflsfjlsfj ls fjls fjlsjflsj flsjfljsfljl j lven", @"eight", @"nine", @"ten", nil];
_tableView = [[UITableView alloc] initWithFrame:self.view.frame];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"%@", NSStringFromSelector(_cmd));
return _items.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@", NSStringFromSelector(_cmd));
return [[_items objectAtIndex:indexPath.row] sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(280, MAXFLOAT)].height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@", NSStringFromSelector(_cmd));
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.contentView.backgroundColor = [UIColor yellowColor];
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = [_items objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18.0f];
CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
cell.textLabel.frame = rect;
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@", NSStringFromSelector(_cmd));
NSLog(@"1 %f", cell.contentView.frame.size.width);
NSLog(@"2 %f", cell.contentView.frame.size.height); // 300
NSLog(@"3 %f", cell.textLabel.frame.size.width); // 280
NSLog(@"4 %f", cell.accessoryView.frame.size.width); // 0 not right
}
- (void)dealloc
{
[_tableView release], _tableView = nil;
[_items release], _items = nil;
[super dealloc];
}
/**
tableView:numberOfRowsInSection:
tableView:heightForRowAtIndexPath:
tableView:heightForRowAtIndexPath:
tableView:heightForRowAtIndexPath:
tableView:heightForRowAtIndexPath:
tableView:heightForRowAtIndexPath:
tableView:heightForRowAtIndexPath:
tableView:heightForRowAtIndexPath:
tableView:heightForRowAtIndexPath:
tableView:heightForRowAtIndexPath:
tableView:heightForRowAtIndexPath:
tableView:cellForRowAtIndexPath:
tableView:willDisplayCell:forRowAtIndexPath:
1 300.000000
2 22.000000
3 280.000000
4 0.000000
tableView:cellForRowAtIndexPath:
tableView:willDisplayCell:forRowAtIndexPath:
1 300.000000
2 66.000000
3 280.000000
4 0.000000
tableView:cellForRowAtIndexPath:
tableView:willDisplayCell:forRowAtIndexPath:
1 300.000000
2 110.000000
3 280.000000
4 0.000000
tableView:cellForRowAtIndexPath:
tableView:willDisplayCell:forRowAtIndexPath:
1 300.000000
2 22.000000
3 280.000000
4 0.000000
tableView:cellForRowAtIndexPath:
tableView:willDisplayCell:forRowAtIndexPath:
1 300.000000
2 22.000000
3 280.000000
4 0.000000
tableView:cellForRowAtIndexPath:
tableView:willDisplayCell:forRowAtIndexPath:
1 300.000000
2 22.000000
3 280.000000
4 0.000000
tableView:cellForRowAtIndexPath:
tableView:willDisplayCell:forRowAtIndexPath:
1 320.000000
2 44.000000
3 0.000000
4 0.000000
tableView:cellForRowAtIndexPath:
tableView:willDisplayCell:forRowAtIndexPath:
1 300.000000
2 22.000000
3 280.000000
4 0.000000
tableView:cellForRowAtIndexPath:
tableView:willDisplayCell:forRowAtIndexPath:
1 300.000000
2 22.000000
3 280.000000
4 0.000000
tableView:cellForRowAtIndexPath:
tableView:willDisplayCell:forRowAtIndexPath:
1 300.000000
2 22.000000
3 280.000000
4 0.000000
*/
@end