0
votes

I've a custom cell with labels init in a UITableViewCell.

In my cellForRowAtIndexPath method after setting label content i call sizeToFit on the label to get the correct width and height, but when i scroll in my TableView, the label width reduce on each cells, every time i scroll.

Anyone for help ?

Thanks

My custom UITableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {


    CGRect viewBounds = self.contentView.bounds;
    // marge
    CGFloat margin = 20;

    self.excuse_date = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, 300, 30)];
    self.excuse_date.textColor = [UIColor blackColor];
    self.excuse_date.font = [UIFont fontWithName:@"Arial" size:12.0f];

    [self addSubview:self.excuse_date];


    static CGFloat dateWidth = 130;
    self.excuse_date.frame = CGRectMake(viewBounds.size.width-dateWidth-35, margin, dateWidth, 20);
    CGRect dateFrame = self.excuse_date.frame;
    self.excuse_date.textAlignment = NSTextAlignmentRight;


    // setup excuse_auteur
    self.excuse_auteur = [[UILabel alloc] init];

    self.excuse_auteur.font = [UIFont fontWithName:@"Helvetica" size:13];
    self.excuse_auteur.textColor = [UIColor darkGrayColor];
    self.excuse_auteur.textAlignment = NSTextAlignmentLeft;

    self.excuse_auteur.frame = CGRectMake(margin, margin, viewBounds.size.width-dateWidth-(margin*2), 20);
    [self addSubview:self.excuse_auteur];


    // setup excuse_texte
    self.excuse_texte = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(dateFrame)+margin/2, viewBounds.size.width - (margin*3), 200)];
    self.excuse_texte.lineBreakMode = NSLineBreakByWordWrapping;
    self.excuse_texte.numberOfLines = 0;
    self.excuse_texte.font = [UIFont fontWithName:@"Helvetica" size:17];
    self.excuse_texte.autoresizingMask = UIViewAutoresizingNone;
    [self addSubview:self.excuse_texte];
    CGRect textFrame = self.excuse_texte.frame;


    // setup up image
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"up-small"]];
    self.img = imageView;
    [self addSubview:self.img];
    imageView.frame = CGRectMake(20, CGRectGetMaxY(textFrame)+margin/2, 20, 20);
    // setup up
    self.up = [[UILabel alloc] initWithFrame:CGRectMake(45, CGRectGetMaxY(textFrame)+margin/2+2, viewBounds.size.width - margin*2, 20)];
    self.up.font = [UIFont fontWithName:@"Helvetica" size:11];
    self.up.textColor = [UIColor darkGrayColor];
    self.up.textAlignment = NSTextAlignmentLeft;
    [self addSubview:self.up];




 }
return self;
}

My cellForRowAtIndexPath method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

BBExcuse *excuse = [self.list objectAtIndex:indexPath.row];


static NSString *cellIdentifier = @"Cell";

// Similar to UITableViewCell, but
BBTableViewCell *cell = (BBTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[BBTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}


cell.excuse_texte.text = excuse.excuse_texte;
// @TODO problem here ?
[cell.excuse_texte sizeToFit];

cell.excuse_auteur.text = excuse.excuse_auteur;
cell.excuse_date.text = excuse.excuse_date;
cell.up.text = [NSString stringWithFormat:@"%@", excuse.up];


cell.img.frame = CGRectMake(20, CGRectGetMaxY(cell.excuse_texte.frame)+20/2, 20, 20);
cell.up.frame = CGRectMake(45, CGRectGetMaxY(cell.excuse_texte.frame)+20/2+2, 20, 20);

// setup up


return cell;
}
1

1 Answers

0
votes

Ok solved by removing sizeToFit and specify height and width in cellForRowAtIndexPath

cell.excuse_texte.text = excuse.excuse_texte;


NSString *str = excuse.excuse_texte;
UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font = [UIFont systemFontOfSize:17];
gettingSizeLabel.text = str;
gettingSizeLabel.numberOfLines = 0;
gettingSizeLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize maximumLabelSize = CGSizeMake(260.0f, 9999.0f);

CGSize theSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];
// on resize sans sizetofit
cell.excuse_texte.frame = CGRectMake(cell.excuse_texte.frame.origin.x, cell.excuse_texte.frame.origin.y, cell.excuse_texte.frame.size.width, theSize.height);