【问题标题】:cellForRowAtIndexPath and sizeToFit reduces label width when scrollingcellForRowAtIndexPath 和 sizeToFit 在滚动时减小标签宽度
【发布时间】:2014-09-17 19:03:47
【问题描述】:

我在 UITableViewCell 中有一个带有标签 init 的自定义单元格。

在设置标签内容后的 cellForRowAtIndexPath 方法中,我在标签上调用 sizeToFit 以获得正确的宽度和高度,但是当我在 TableView 中滚动时,每次滚动时,每个单元格的标签宽度都会减小。

有人帮忙吗?

谢谢

我的自定义 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;
}

我的 cellForRowAtIndexPath 方法

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

【问题讨论】:

    标签: ios iphone uitableview


    【解决方案1】:

    通过删除 sizeToFit 并在 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      相关资源
      最近更新 更多