Question

a boy of StackOverflow had given such help to set the height of the cells automatically based on the content of the label ...

I have implemented his method but when I scroll on tavleview, all the text in each cell overlaps and do not read anything ... Can you tell me what 's wrong with this code?

enter image description here

I had to change CGSize because I'm working on some code ios7 was deprecated for example:

 

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

Can you help me please?

- (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;
}
Était-ce utile?

La solution

Solved

- (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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top