如何为表格单元格部分添加左右边距?

时间:2015-11-12 10:21:54

标签: ios

我是iOS的新手,我有一个分组的表格视图,一个部分是一行,如下所示。如何设置单元格的左右边距?

以下是我为边框设置的代码......

[cell.layer setBorderColor:[UIColor colorWithRed:0.00 green:0.60 blue:1.00 alpha:1.0].CGColor];
[cell.layer setBorderWidth:1.0f];
[cell.layer setCornerRadius:5];

What I have now...

预期结果: enter image description here

2 个答案:

答案 0 :(得分:4)

The best programming practise for this is subclassing your UITableViewCell and override its setFrame method.

- (void)setFrame:(CGRect)frame {
    frame.origin.x += 10;
    frame.size.width -= 20;
    [super setFrame:frame];
}

Also, you can set the corner radius and colour of the cell in drawRect method

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    // border radius
    [self.layer setCornerRadius:5.0f];

    // border
    [self.layer setBorderColor:[UIColor colorWithRed:0.00 green:0.60 blue:1.00 alpha:1.0].CGColor];
    [self.layer setBorderWidth:1.0f];
}

One more thing, if you just want to make the cell a little more attractive, add this method too in the drawRect:

[self.layer setShadowColor:[UIColor lightGrayColor].CGColor];
[self.layer setShadowOpacity:0.8];
[self.layer setShadowRadius:3.0];
[self.layer setShadowOffset:CGSizeMake(2.0, 2.0)];

答案 1 :(得分:0)

喜欢

步骤-1

创建一个名为UIView的公共BackgroundView。并将子视图设为cell.contentView

第2步

根据您的需要在BackgroundView设置框架。基于内容视图宽度

第3步

LabelDateImage添加到BackgroundView的子视图中。

步骤-4

然后设置BackgroundView的图层,最后得到答案

[cell.BackgroundView setBorderColor:[UIColor colorWithRed:0.00 green:0.60 blue:1.00 alpha:1.0].CGColor];
[cell.BackgroundView setBorderWidth:1.0f];
[cell.BackgroundView setCornerRadius:5];