2017-02-20 178 views
3

我有基本的表格视图单元格,我想在它们之间设置一些间距。在基本表格视图单元格之间添加间距

enter image description here

我试图

cell.contentView.layoutMargins.top = 8 

cell.layoutMargins.top = 8 

,但没有奏效。我知道我可以创建一个自定义单元格,但我想知道如何以这种方式添加边距。

编辑:

然后,我改变了单元格样式定制:

enter image description here

,并增加了一个观点,所以我可以设置一些约束它

enter image description here

但结果仍然相同,没有间隔下注细胞,它有点忽略了自定义视图,我猜是因为它需要细胞的自定义类,但我不想这样做。

enter image description here

+0

只需创建你的包含单元格视图+额外空间视图(即它具有相同的颜色作为背景),那么它看起来像间距细胞 – Tj3n

+0

的最简单,最简单的办法之间我在底部添加了一个自定义视图 – Koushik

回答

4

不要添加间距细胞本身之间,而是添加自定义containerView细胞的contentView与顶部和/或底部的限制。

enter image description here

+0

这听起来不错,但它给出了一个错误,即Container视图不能放置在运行时重复的视图中。然后,我将单元格配置从“基本”更改为“自定义”,我在“内容视图”中添加了一个视图并使用约束进行播放,但没有任何效果。 – fullMoon

+0

@fullMoon请将您的布局添加到问题中,以便我可以找到问题。 – alexburtnik

0

您实现小区之间的间距增加是使numberOfSections = array.count和使每个部分只包含一行。然后定义headerView和它的高度。

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return cellSpacingHeight 
    } 
+2

这是一种解决方法,但不这样做。表视图部分是为了另一个目的。你会发现自己处于一个尴尬的位置,当你按照这个建议,并在某个时候后真的需要将一些细胞分成几个部分 – alexburtnik

0

如果您有单个节,请尝试交换节中的行数和tableView中的节数。那么对于你们之间的间距,可以使用委托:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 10 
    } 
1

请您尝试一下 你可以在两个单元之间建立头和增加空间

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 
} 
0

一般来说,一个UITableViewCell的还是它的contentView工作的layoutMargins容易出错。根据我的经验,单元格和contentViewlayoutMargins在表重新加载期间的几个点都会重置。有两种解决方法:

  1. 使用间隔单元格。例如,只需创建一个在contentView上设置了特定高度约束的透明UITableViewCell子类。
  2. 使用透明的UITableViewCell子类,并将您自己的(可能不透明的)子视图添加到contentView,并将顶部/底部约束设置为所需间距的1/2。您可以覆盖setHighlighted:animated以控制单元在选择上的外观。这是我在屏幕截图中使用的方法。它也适用于在左侧/右侧添加边距。

enter image description here enter image description here

相关问题