- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 列宽
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何种字体进行显示
UIFont *font = [UIFont systemFontOfSize:13];
NSString *content = [data objectAtIndex:indexPath.row];
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
return size.height;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
// 列宽
CGFloat contentWidth = self.tableView.frame.size.width;
UIFont *font = [UIFont systemFontOfSize:13];
NSString *content = [data objectAtIndex:indexPath.row];
// 计算出显示完内容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
// 構建顯示行
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
rect.size = size;
cell.textLabel.frame = rect;
cell.textLabel.text = content;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = font;
return cell;
}