xcode 自定义 UITableViewCell - 在每个单元格之间添加边距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19640050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom UITableViewCell - Add margin between each cell
提问by Dancer
Is it possible to apply a margin between UITableView cells?
是否可以在 UITableView 单元格之间应用边距?
The brief requires the table cells to look like below -
简介要求表格单元格如下所示 -
I have a custom cell class to style the background / fonts / separate labels - but cannot figure out how to apply any kind of spacing!
我有一个自定义单元格类来设置背景/字体/单独标签的样式 - 但无法弄清楚如何应用任何类型的间距!
回答by dariaa
I woud do it the following way: first, make sure background
color for both your UITableViewCell
and its contentView
is [UIColor clearColor]
, then add a subview slightly smaller than your cell contentView
(so that height = CGRectGetHeight(contentView) - margin
) and fill it with the color you need. Then set the backgroundView
for your UITableView
and this should do the trick.
我会按照以下方式进行操作:首先,确保background
您UITableViewCell
和它的颜色都contentView
为[UIColor clearColor]
,然后添加一个比您的单元格略小的子视图contentView
(以便height = CGRectGetHeight(contentView) - margin
)并用您需要的颜色填充它。然后backgroundView
为您设置UITableView
,这应该可以解决问题。
回答by mano
The Best way to get space between two cells in TableView in Swift language.
使用 Swift 语言在 TableView 中的两个单元格之间获得空间的最佳方法。
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.contentView.backgroundColor=UIColor.clearColor()
var whiteRoundedCornerView:UIView!
whiteRoundedCornerView=UIView(frame: CGRectMake(5,10,self.view.bounds.width-10,120))
whiteRoundedCornerView.backgroundColor=UIColor(red: 174/255.0, green: 174/255.0, blue: 174/255.0, alpha: 1.0)
whiteRoundedCornerView.layer.masksToBounds=false
whiteRoundedCornerView.layer.shadowOpacity = 1.55
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(1, 0);
whiteRoundedCornerView.layer.shadowColor=UIColor(red: 53/255.0, green: 143/255.0, blue: 185/255.0, alpha: 1.0).CGColor
whiteRoundedCornerView.layer.cornerRadius=3.0
whiteRoundedCornerView.layer.shadowOffset=CGSizeMake(-1, -1)
whiteRoundedCornerView.layer.shadowOpacity=0.5
cell.contentView.addSubview(whiteRoundedCornerView)
cell.contentView.sendSubviewToBack(whiteRoundedCornerView)
}
回答by idris y?ld?z
for the margin of the cell inside cell class's layoutSubviews() funs add this code line.
对于单元格类的 layoutSubviews() funs 内单元格的边距,添加此代码行。
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))