6

在此处输入图像描述

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

我创建了一个 的子类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 个像素。我认为这段代码应该实现我的目标,但它没有。模拟器中的视图没有变化,就像我没有写任何代码一样。

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

4

3 回答 3

6

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

如何以分组样式设置 UITableView 中单元格的宽度

斯威夫特版本

于 2013-10-29T07:26:18.473 回答
3

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

如果您的自动布局由于设置框架而损坏,您可以添加一个UIView宽度比单元格短contentViewcontentView,并将您的其他 UI 元素放在视图上。

通过执行以下设置,您可以获得具有“水平填充”的单元格:

cell.backgroundColor = UIColor.clearColor()
cell.contentView.backgroundColor = UIColor.clearColor()
于 2015-11-07T16:30:35.953 回答
2

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

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

于 2014-09-10T16:01:03.403 回答