2015-11-12 126 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太添加此方法
相關問題