【问题标题】:change UILabel frame dynamically in custom TableViewCell在自定义 TableViewCell 中动态更改 UILabel 框架
【发布时间】:2012-03-07 14:47:24
【问题描述】:

我编写了一个 UITableViewcell 的子类,它由各种 UILabel 组成,我在

中定义了 UIlabel 的样式
  • (id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString*)reuseIdentifier

和框架中的标签框

  • (void)layoutSubviews

现在,在为 UILabel 定义静态矩形之后,我想修改 UILabel 的矩形,并编写一个函数来修改框架标签,但调用此函数不会修改 UILabel 的框架。
如果有人知道如何实现标签的动态框架,请分享。

【问题讨论】:

  • 我对你问题的措辞有点困惑。你想改变矩形而不是框架?我不确定这是否有意义。
  • 如果你只需要设置标签的x坐标,那么你可以看看我的github project's floating list

标签: iphone objective-c ios xcode uitableview


【解决方案1】:

我通常实现这一点的方法是实现一个自定义 tableviewcell 并在我指定标签大小的子视图中实现我自己的 drawRect 方法,如下所示:

@implementation CompositeSubviewBasedRestaurantCellContentView

- (id)initWithFrame:(CGRect)frame cell:(RestaurantCell *)cell
{
    if (self = [super initWithFrame:frame]) {
        _cell = cell;

        self.opaque = YES;
        self.backgroundColor = _cell.backgroundColor;
    }

    return self;
}
- (void)drawRect:(CGRect)rect
{
    _highlighted ? [[UIColor whiteColor] set] : [[UIColor blackColor] set];
    [_cell.name drawAtPoint:CGPointMake(11.0, 22.0) withFont:[UIFont boldSystemFontOfSize:17.0]];
}

我认为 Apple 的示例非常好,例如 Table View Suite:

https://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007318

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 2018-11-28
    • 2014-06-23
    • 1970-01-01
    相关资源
    最近更新 更多