如何将UITableView单元格的大小更改为NSString的大小?我尝试了以下内容,单元格的大小略有变化,但未达到文本的完整高度。
我有:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *str = [_recipe.ingredients objectAtIndex:indexPath.row];
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
UIFont *cellFont = [UIFont systemFontOfSize:14];
CGSize constraintSize = CGSizeMake(screenWidth-40, MAXFLOAT);
CGSize labelSize = [str sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height +35;
}
并且:
// Customize the appearance of table view cells.
- (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];
}
if(indexPath.section == 0){
cell.textLabel.text = [_recipe.ingredients objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
cell.textLabel.numberOfLines=1;
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
}
//cell.textLabel.text = [NSString stringWithFormat:@"Cell Row #%d", [indexPath row]];
return cell;
}
我收到两个警告UILineBreakModeTailTruncated已弃用。
我如何得到要包装的词语。
答案 0 :(得分:0)
您想使用[str sizeWithFont:]
。此方法还有一些其他变体,它们考虑了约束宽度和换行模式。在这里查找可行的内容:http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html