【问题标题】:UITableViewCell height not adjusting until user scrollsUITableViewCell 高度在用户滚动之前不会调整
【发布时间】: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


【解决方案1】:

所以我不小心为表设置了两次数据源,而不是设置委托。

【讨论】:

    猜你喜欢
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多