【问题标题】:How to change separator height in UITableView Swift 3?如何在 UITableView Swift 3 中更改分隔符高度?
【发布时间】:2017-11-26 10:53:49
【问题描述】:

虽然已经有一些关于这个主题的答案。它们都没有涵盖 Swift 3,它们来自很久以前。目前在 Swift 3 中更改 UITableView 中分隔符高度的最佳方法是什么?

【问题讨论】:

  • 您可以使用单元格作为分隔符并设置分隔符单元格的高度。就像 cell-separatorCelll-cell-...
  • 检查separatorInset的值

标签: ios swift uitableview swift3 list-separator


【解决方案1】:

为 Swift 3 更新:

如果要更改 UITableView 分隔符的高度,请使用以下代码。
您应该将其添加到 UITableViewCell 方法 awakeFromNib() 以避免重新创建。

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    let mScreenSize = UIScreen.main.bounds
    let mSeparatorHeight = CGFloat(3.0) // Change height of speatator as you want
    let mAddSeparator = UIView.init(frame: CGRect(x: 0, y: self.frame.size.height - mSeparatorHeight, width: mScreenSize.width, height: mSeparatorHeight))
    mAddSeparator.backgroundColor = UIColor.brown // Change backgroundColor of separator
    self.addSubview(mAddSeparator)
}

【讨论】:

  • 嘿——这实际上是在单元格内放置了一个 UIView,看起来像一个更厚的分隔符,但实际上它只是占用了单元格内的空间。有没有办法真正增加隔板的厚度?
  • @richc 您可以创建一个自定义单元格,您可以在其中创建一个包含所有单元格组件的视图,该视图将小于整个自定义单元格的内容视图。可以借助自定义单元格中的约束来设置分隔符高度。
  • 这不会改变分隔符的高度,而是在单元格中插入另一个视图。这不是正确的答案
【解决方案2】:

这是正确的做法。

首先,您应该在 ViewController 中设置 (tableView.separatorStyle = .none)

import UIKit 

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.separatorStyle = .none
   }

}

其次,在您的 TableViewCell 类中,您应该创建一个 separatorView。 并且不要忘记为您的单元格继承 TableViewCell 类。

class TableViewCell: UITableViewCell {
   let separator = UIView()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    separator.backgroundColor = .black
    contentView.addSubview(separator)
}
   override func layoutSubviews() {
      super.layoutSubviews()

      //Your separatorLineHeight with scalefactor 
      let separatorLineHeight: CGFloat = 1/UIScreen.main.scale

      separator.frame = CGRect(x: self.contentView.frame.origin.x, 
                               y: self.contentView.frame.size.height - separatorLineHeight,
                           width: self.contentView.frame.size.width,
                          height: separatorLineHeight)
   }

}

最后,你得到了一条细的分隔线,当然,你可以随意增加这个值。

【讨论】:

  • 如果有人需要 Objective-C 版本,请在这里评论
  • laytouSubviews() 方法被多次调用,因此 UIView 作为分隔符将被创建多次。这不是很好也不是很有效。
  • 是的,你是对的。我会更正这段代码。那是很久以前的事了:)
【解决方案3】:

对于那些想要使用自动布局的人来说,这里是代码

var additionalSeparator:UIView = UIView()
override func awakeFromNib() {
        super.awakeFromNib()
        self.createSeparator()
    }
    func createSeparator() {

        self.additionalSeparator.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(self.additionalSeparator)
    }
    func setConstraintForSeparator() {
        self.additionalSeparator.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: self.separatorInset.left).isActive = true
        self.additionalSeparator.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -self.separatorInset.right).isActive = true
        self.additionalSeparator.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0).isActive = true
        self.additionalSeparator.heightAnchor.constraint(equalToConstant: 1).isActive = true
        self.additionalSeparator.backgroundColor = UIColor.greyishBrown
    }

【讨论】:

    【解决方案4】:

    试试这个 Swift 3:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: YOUR_CELL_IDENTIFIER, for: indexPath) as! yourTableViewCell
    
        let viewSeparatorLine = UIView(frame:CGRect(x: 0, y: cell.contentView.frame.size.height - 5.0, width: cell.contentView.frame.size.width, height: 5))
        viewSeparatorLine.backgroundColor = .red
        cell.contentView.addSubview(viewSeparatorLine)
        return cell
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2013-07-16
      • 2017-04-06
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 2018-07-06
      相关资源
      最近更新 更多