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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:12:35  来源:igfitidea点击:

Custom UITableViewCell - Add margin between each cell

objective-cxcode

提问by Dancer

Is it possible to apply a margin between UITableView cells?

是否可以在 UITableView 单元格之间应用边距?

The brief requires the table cells to look like below -

简介要求表格单元格如下所示 -

enter image description here

在此处输入图片说明

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 backgroundcolor for both your UITableViewCelland its contentViewis [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 backgroundViewfor your UITableViewand this should do the trick.

我会按照以下方式进行操作:首先,确保backgroundUITableViewCell和它的颜色都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))