【问题标题】:height For Row At Index Path not correct display索引路径处的行的高度显示不正确
【发布时间】:2013-10-05 11:20:15
【问题描述】:

StackOverflow 的一个男孩提供了这样的帮助,可以根据标签的内容自动设置单元格的高度......

我已经实现了他的方法,但是当我在 tavleview 上滚动时,每个单元格中的所有文本都重叠并且没有读取任何内容......你能告诉我这段代码有什么问题吗?

我不得不更改 CGSize,因为我正在处理一些代码 ios7 已被弃用,例如:

 

 CGSize size = [comment sizeWithFont: [UIFont systemFontOfSize: 14] constrainedToSize: lineBreakMode constraint: UILineBreakModeWordWrap];

你能帮帮我吗?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{

    PFObject *object = [self objectAtIndexPath:indexPath];
    //Here you are getting the comment again.  This is necessary to determine how tall you need
    //the cell to be
    NSString *comment = [object objectForKey:(@"Testo")];

    // Again you are getting the constraints because you are going to us this size
    // to constrain the height of the cell just like you determined the size for the label.
    CGSize constraint = CGSizeMake(250 - (10 * 2), 10000.0f);

    // Again, determining the size that we will need for the label, because it will drive
    // the height of the cell
    UIFont *FONT = [UIFont systemFontOfSize:11];

     CGSize size = [comment boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT }context:nil].size;

    //  Finally, we can determine the height for the cell!
    CGFloat height = MAX(size.height, 44.0f);

    //  return the height, which is the height of the label plus some extra space to make
    //  it look good.
    return height + (10 * 2);
}

// Override to customize the look of a cell representing an object. The default is to display
// a UITableViewCellStyleDefault style cell with the label being the first key in the object.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    NSString *text = [object objectForKey:@"Testo"];


    CGSize constraint = CGSizeMake(250 - (10 * 2), 10000.0f);

    // This is determining the size that you will need for the label based on the comment
    // length and the contraint size

    UIFont *FONT = [UIFont systemFontOfSize:11];

    CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT }context:nil].size;

    //  Here you are creating your label and initializing it with the frame that you have
    //  determined by your size calculations above
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, MAX(size.height, 44.0f) + 20.0f)];

    // setting up the label properties


    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.text = text;
    label.font = [UIFont systemFontOfSize:11];

    // adding the label view to the cell that you have created
    [cell.contentView addSubview:label];

    // Configure the cell


    return cell;
}

【问题讨论】:

    标签: ios height tableview row-height


    【解决方案1】:

    解决了

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    {
        NSString *comment = [[self.objects objectAtIndex:[indexPath row]] objectForKey:@"Testo"];
        CGFloat whidt =  260;
        UIFont *FONT = [UIFont systemFontOfSize:15];
        NSAttributedString *attributedText =[[NSAttributedString alloc]  initWithString:comment  attributes:@  { NSFontAttributeName: FONT }];
        CGRect rect = [attributedText boundingRectWithSize:(CGSize){whidt, MAXFLOAT}
                                                   options:NSStringDrawingUsesLineFragmentOrigin
                                                   context:nil];
        CGSize size = rect.size;
        return size.height +130;
    }
    
    - (FFCustomCellTimeline *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    
        FFCustomCellTimeline *cell = (FFCustomCellTimeline * )[self.tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (!cell) {
            cell = [[FFCustomCellTimeline alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }
    
    
        NSString *text = [object objectForKey:@"Testo"];
        CGSize constraint = CGSizeMake(260 , 20000.0f);
        UIFont *FONT = [UIFont systemFontOfSize:15];
        CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT }context:nil].size;
    
    
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多