2015-11-12 78 views
1

我是新来的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

+0

self.tableView.contentInset = UIEdgeInsetsMake(0,20,0,20) –

回答

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]; 
+0

感谢您的步骤,我有一个问题:实际上我需要内容视图和配件视图在同一个边框内。如果将边框设置为背景视图,是否还包括附件视图? – yun

+0

@yun - 在你的问题是好的,你可以添加像创建一个imageview添加图像的功能,否则看到这个链接http://stackoverflow.com/questions/2876041/can-a-standard-accessory-view-在一个不同的位置,在一个uitableviewcel –

+0

好主意!我只是使用箭头图像来替换附件视图 – yun

4

最好的编程实践,这是继承您的UITableViewCell并覆盖其SETFRAME方法。

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

此外,您还可以设置单元格的圆角半径和颜色的drawRect方法

- (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]; 
} 

还有一件事,如果你只是想使细胞多了几分吸引力

[self.layer setShadowColor:[UIColor lightGrayColor].CGColor]; 
[self.layer setShadowOpacity:0.8]; 
[self.layer setShadowRadius:3.0]; 
[self.layer setShadowOffset:CGSizeMake(2.0, 2.0)]; 
:在 的drawRect太添加此方法
相关问题