【问题标题】:iOS - add horizontal padding to tableview cellsiOS - 向表格视图单元格添加水平填充
【发布时间】:2013-10-29 03:31:13
【问题描述】:

我想为我的表格视图单元格添加水平填充,如图所示。

我创建了UITableViewCell的子类,其宽度为314像素(屏幕宽度为320),并在initWithCoder方法中设置它的框架:

[self customizeXPosition:self.profileImage];
[self customizeXPosition:self.birthdayLabel];
[self customizeXPosition:self.heightLabel];
[self customizeXPosition:self.weightLabel];

这是我的- (void)customizeXPosition:(UIView *)view

- (void)customizeXPosition:(UIView *)view {
    CGRect tmpRect = view.frame;
    tmpRect.origin.x += 3.0;
    view.frame = tmpRect;
}

自定义单元格比屏幕小,我将单元格中的每个元素向右移动了 3 个像素。我认为这段代码应该实现我的目标,但它没有。模拟器中的视图没有变化,就像我没有写任何代码一样。

我怎样才能实现我的目标?

【问题讨论】:

标签: ios uitableview


【解决方案1】:

尝试按照这篇文章覆盖 UITableViewCell 中的 -setFrame 方法:

How to set the width of a cell in a UITableView in grouped style

Swift version

【讨论】:

  • btw @Brian,我认为最好将其作为答案,但转到该 SO 页面并查看用户的答案。我不想因为他给出的完美答案而受到赞扬。他也帮了我! :) 对于看到此答案的其他人,如果该答案对您有帮助,请投票给他的答案而不是我。 :)
【解决方案2】:

@faterpig 在很多情况下都是正确的。但是,设置框架可能会破坏自动布局。

如果您的自动布局由于设置框架而损坏,您可以将宽度小于单元格contentViewUIView 添加到contentView 并将您的其他 UI 元素放在视图上。

通过如下设置,可以获得带有“水平填充”的单元格:

cell.backgroundColor = UIColor.clearColor()
cell.contentView.backgroundColor = UIColor.clearColor()

【讨论】:

    【解决方案3】:

    这适用于在视图中填充内容:

    yourView.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 0);

    【讨论】:

    • 哎呀,太丑了! :D
    猜你喜欢
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    相关资源
    最近更新 更多