【发布时间】:2015-03-12 03:34:44
【问题描述】:
我正在尝试用文本填充 cell.textLabel。该文本在数组中每个对象的行数上有所不同,因此单元格需要调整高度。在用户滚动列表之前,单元格不会调整高度。
在代码中。
IB的图片
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
cell.textLabel.text = [kanyeLines objectAtIndex:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText =[kanyeLines objectAtIndex:indexPath.row];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:cellText
attributes:@
{
NSFontAttributeName: cellFont
}];
CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return rect.size.height + 20;
}
【问题讨论】:
-
你在使用故事板吗?
-
@AshrafTawfeeq 是的,我是。
-
我猜你没有实现 *heightForRow...` 或
estimatedHeightForRow...。 -
已实现。可以在上面的代码中看到。
标签: ios objective-c uitableview