在基本表格视图单元格之间添加间距
我有基本的表格视图单元格,我想在它们之间设置一些间距。在基本表格视图单元格之间添加间距
我试图
cell.contentView.layoutMargins.top = 8
和
cell.layoutMargins.top = 8
,但没有奏效。我知道我可以创建一个自定义单元格,但我想知道如何以这种方式添加边距。
编辑:
然后,我改变了单元格样式定制:
,并增加了一个观点,所以我可以设置一些约束它
但结果仍然相同,没有间隔下注细胞,它有点忽略了自定义视图,我猜是因为它需要细胞的自定义类,但我不想这样做。
这听起来不错,但它给出了一个错误,即Container视图不能放置在运行时重复的视图中。然后,我将单元格配置从“基本”更改为“自定义”,我在“内容视图”中添加了一个视图并使用约束进行播放,但没有任何效果。 – fullMoon
@fullMoon请将您的布局添加到问题中,以便我可以找到问题。 – alexburtnik
您实现小区之间的间距增加是使numberOfSections = array.count和使每个部分只包含一行。然后定义headerView和它的高度。
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return cellSpacingHeight
}
这是一种解决方法,但不这样做。表视图部分是为了另一个目的。你会发现自己处于一个尴尬的位置,当你按照这个建议,并在某个时候后真的需要将一些细胞分成几个部分 – alexburtnik
如果您有单个节,请尝试交换节中的行数和tableView中的节数。那么对于你们之间的间距,可以使用委托:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 10
}
请您尝试一下 你可以在两个单元之间建立头和增加空间
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return array.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 1 // Because we need to space between two cell
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
//Configure your cell
let cel:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!
cel.textLabel?.text = array[indexPath.section] //please use section because we have only one row for all section
return cel
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
return 15 // whatever you want space between two cell
}
一般来说,一个UITableViewCell
的还是它的contentView
工作的layoutMargins
容易出错。根据我的经验,单元格和contentView
的layoutMargins
在表重新加载期间的几个点都会重置。有两种解决方法:
- 使用间隔单元格。例如,只需创建一个在contentView上设置了特定高度约束的透明UITableViewCell子类。
- 使用透明的
UITableViewCell
子类,并将您自己的(可能不透明的)子视图添加到contentView
,并将顶部/底部约束设置为所需间距的1/2。您可以覆盖setHighlighted:animated
以控制单元在选择上的外观。这是我在屏幕截图中使用的方法。它也适用于在左侧/右侧添加边距。
只需创建你的包含单元格视图+额外空间视图(即它具有相同的颜色作为背景),那么它看起来像间距细胞 – Tj3n
的最简单,最简单的办法之间我在底部添加了一个自定义视图 – Koushik