高度对于行索引路径显示不正确

时间:2013-09-29 15:44:13

标签: ios height tableview row-height

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

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

enter image description here

我不得不更改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;
}

1 个答案:

答案 0 :(得分:0)

解决

- (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;
}