【问题标题】:Unable to change size of UILabel in custom UITableViewCell无法在自定义 UITableViewCell 中更改 UILabel 的大小
【发布时间】:2015-04-06 14:51:47
【问题描述】:
if (indexPath.row == 2)
{
        RestaurantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RestaurantCell"];
        if (!cell)
        {
            [tableView registerNib:[UINib nibWithNibName:@"RestaurantCell" bundle:nil] forCellReuseIdentifier:@"RestaurantCell"];
            cell = [tableView dequeueReusableCellWithIdentifier:@"RestaurantCell"];
            CAGradientLayer *gradient = [CAGradientLayer layer];
            gradient.frame = cell.gradientView.bounds;
            gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor]CGColor], nil];
            [cell.gradientView.layer insertSublayer:gradient atIndex:0];
            cell.gradientView.alpha = 0.9;
        }

        NSData *imgData2 = [NSData dataWithData:restaurantPhotoArray[1]];
        cell.restaurant2Photo.image = [UIImage imageWithData:imgData2];
        cell.restaurant2Photo.contentMode = UIViewContentModeScaleAspectFill;
        [cell.restaurant2Photo setClipsToBounds:YES];

        NSDictionary *morning2 = [[NSDictionary alloc]initWithDictionary:NorthRestaurantsArray[1]];
        cell.restaurant2Name.text = morning2[@"name"];
        cell.restaurant2Name.numberOfLines = 0;
        [cell.restaurant2Name sizeToFit];
}

我有一个自定义单元格,它使用上面的代码在 cellForRowAtIndexPath 中实现。显示表格时,restaurant2Photo 显示指定的图像,并将正确的文本设置为 restaurant2Name,但 sizeToFit 对标签没有影响,因此其大小保持不变,如 .xib 文件中所定义。我有另一个自定义单元格显示在 indexPath 0 和 1 处,其中 sizeToFit 工作正常,但是当它到达 indexPath 2 和 RestaurantTableViewCell sizeToFit 时不起作用。有没有人能提出为什么会发生这种情况?提前感谢您的帮助:)

【问题讨论】:

  • 您在 Indexpath 0 和 1 加载的单元格是在 XIB 中构建的?
  • 是的,和 indexPath 2 完全一样
  • 您是否检查过 contentView 的超级视图有 0 px 的尾边距和 0 px 的下边距?

标签: ios objective-c uitableview


【解决方案1】:

您的 nib 文件可能启用了 AutoLayout

在您的自定义单元格awakeFromNib: 方法中写入以下内容

[self.restaurant2Name setTranslatesAutoresizingMaskIntoConstraints:YES];

这将为您完成这项工作。

【讨论】:

  • 非常感谢 :D 我完全忘记了在我第一次创建 .xib 文件时启用了 AutoLayout
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 2016-05-15
  • 2011-01-18
  • 2010-12-04
  • 1970-01-01
相关资源
最近更新 更多