- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.tag = 1;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.highlightedTextColor = [UIColor whiteColor];
label.numberOfLines = 0;
label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制
//label.backgroundColor = [UIColor clearColor];
label.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:label];
[label release];
}
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = "text\n说话不算话\nobjective";
//===== 设置 Label间距和 Cell高度 =====
static const NSUInteger DEFAULT_ROW_HEIGHT = 44;//默认行高
static const NSUInteger INTERVAL_X_Y = 2;//四边间距,正数则向内缩进
//设置 Label间距
CGRect cellFrame = [cell frame];
cellFrame.origin = CGPointMake(0, 0);
CGRect labelFrame = CGRectInset(cellFrame, INTERVAL_X_Y, INTERVAL_X_Y);
label.frame = labelFrame;
[label sizeToFit];
//设置 Cell高度
if (label.frame.size.height > (DEFAULT_ROW_HEIGHT + INTERVAL_X_Y))
{
cellFrame.size.height = label.frame.size.height + INTERVAL_X_Y + INTERVAL_X_Y;
}
[cell setFrame:cellFrame];
//===== 设置 Label间距和 Cell高度 =====
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}