如何增加UITableView分隔符高度?

vin*_*nay 48 iphone uitableview

我希望每个之间有更多的空间(10px)cell.我怎样才能做到这一点?

我添加了这段代码

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Run Code Online (Sandbox Code Playgroud)

Dmi*_*iov 25

对我来说最好的方法,只需在cellForRowAtIndexPath或willDisplayCell中添加它

CGRect sizeRect = [UIScreen mainScreen].applicationFrame;    
NSInteger separatorHeight = 3;
UIView * additionalSeparator = [[UIView alloc] initWithFrame:CGRectMake(0,cell.frame.size.height-separatorHeight,sizeRect.size.width,separatorHeight)];
additionalSeparator.backgroundColor = [UIColor grayColor];
[cell addSubview:additionalSeparator];
Run Code Online (Sandbox Code Playgroud)

对于Swift 3.0:

let screenSize = UIScreen.main.bounds
let separatorHeight = CGFloat(3.0)
let additionalSeparator = UIView.init(frame: CGRect(x: 0, y: self.frame.size.height-separatorHeight, width: screenSize.width, height: separatorHeight))
additionalSeparator.backgroundColor = UIColor.gray
self.addSubview(additionalSeparator)
Run Code Online (Sandbox Code Playgroud)

您应该将其添加到单元格的方法awakeFromNib()以避免重新创建.

  • 在再次添加之前,您需要检查是否添加了`additionalSeparator`,因为cellForRow ...和willDisplayCell可以为单个单元格多次调用,因为单元格重用.当用户越来越多地滚动表格时,这将导致您的性能下降. (4认同)

Ear*_*rey 22

我见过许多笨重的解决方案,比如UITableView使用隐藏单元格进行子类化,以及其他不太理想的解决方案.在这个帖子里.

初始化时UITableView,将rowHeight属性设置为UITableView等于=单元格高度+所需分隔符/空间高度的高度.

UITableViewCell但是,不要使用标准类,而是继承UITableViewCell类并重写其layoutSubviews方法.在调用之后super(不要忘记),将单元格本身的高度设置为所需的高度.

奖金更新18/OCT/2015:

你可以对此有点聪明.上面的解决方案基本上将"分隔符"放在单元格的底部.真正发生的是,行高由TableViewController管理,但是单元格的大小调整得有点低.这导致分隔符/空白空间位于底部.但您也可以垂直居中所有子视图,以便在顶部和底部留出相同的空间.例如1pt和1pt.

您还可以在单​​元子类上创建isFirst,isLast便捷属性.您可以在cellForRowAtIndexPath中将它们设置为yes.这样,您可以处理layoutSubviews方法中顶部和底部分隔符的边缘情况,因为这可以访问这些属性.通过这种方式,您可以处理顶部或底部的边缘情况 - 因为有时设计部门需要N + 1个分隔符,而单元格数量仅为N.因此您必须以特殊方式处理顶部或引导部分.但最好在单元格中执行此操作,而不是tableViewHeader或TableViewFooter.


May*_*jak 12

我不认为使用标准API是可能的.我想你需要继承UITableViewCell并添加一个模拟单元格底部分隔符的视图.

您可能想要检查这个问题,它似乎相关并且有一些示例代码: iPhone + UITableView +为分隔符放置图像


Kin*_*ard 7

斯威夫特

对我来说,最简单和最简单的办法是添加下面的代码片段cellForRowAtIndexPath或在willDisplayCell:

override func tableView(tableView: UITableView,
                        willDisplayCell cell: UITableViewCell,
                        forRowAtIndexPath indexPath: NSIndexPath)
{
        let additionalSeparatorThickness = CGFloat(3)
        let additionalSeparator = UIView(frame: CGRectMake(0,
                                                           cell.frame.size.height - additionalSeparatorThickness,
                                                           cell.frame.size.width,
                                                           additionalSeparatorThickness))
        additionalSeparator.backgroundColor = UIColor.redColor()
        cell.addSubview(additionalSeparator)
}
Run Code Online (Sandbox Code Playgroud)

  • 不要像上面那样添加细胞!您为每个"重复"关闭单元格添加子视图.在添加之前标记子视图并获取viewWithTag:TAG:如果为nil,则继续标记和添加.对于标签,请参阅此处:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html (7认同)

Kon*_*pen 6

您可以在故事板中完全执行此操作.方法如下:

  • 转到故事板并选择tableview
  • 显示大小检查器,并从那里设置行高度为140.
  • 然后显示属性检查器,并从那里将分隔符设置为单行和样式平原并选择一种颜色
  • 然后在故事板(或文档大纲)中选择单元格
  • 再次在"大小检查器"中的"表视图单元格"下,将自定义行高设置为120.

就这样.你的分隔符将是20个单位高.

  • 使用Swift + iOS 8.3 + Xcode版本6.3.2不适用于动态单元格. (10认同)

poc*_*shi 5

这是相当古老的。不过我会发布我的方法。

只需稍微增加单元格高度并为单元格分配一个遮罩层,如下所示:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "...", for: indexPath)

    // Configure the cell...
    let maskLayer = CAShapeLayer()
    let bounds = cell.bounds
    maskLayer.path = UIBezierPath(roundedRect: CGRect(x: 2, y: 2, width: bounds.width-4, height: bounds.height-4), cornerRadius: 5).cgPath
    cell.layer.mask = maskLayer

    return cell
}
Run Code Online (Sandbox Code Playgroud)

所以在这个例子中,我的分隔符高度为 4。

玩得开心!