【问题标题】:Is it possible to increase the UITableView separator line thickness?是否可以增加 UITableView 分隔线的粗细?
【发布时间】:2015-04-23 04:13:35
【问题描述】:

我到处找。试图增加这条线的粗细。反正有没有以编程方式做到这一点?谢谢

【问题讨论】:

标签: ios xcode uitableview


【解决方案1】:

我对此的解决方案是向 UIView 或 UITableViewCell 添加扩展。

extension UIView {

func addSeparator(ofHeight height : CGFloat) {
    let lineView = UIView()
    lineView.backgroundColor = .red
    self.addSubview(lineView)
    let constraintString = "V:|-\(self.frame.size.height - height)-[v0(\(height))]|"
    self.addConstraintsWithFormat("H:|[v0]|", views: lineView)
    self.addConstraintsWithFormat(constraintString, views: lineView)
}

//MARK: - Constraints Extension

func addConstraintsWithFormat(_ format: String, views: UIView...) {
    var viewsDictionary = [String: UIView]()
    for (index, view) in views.enumerated() {
        let key = "v\(index)"
        view.translatesAutoresizingMaskIntoConstraints = false
        viewsDictionary[key] = view
    }
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
} }

然后在您的自定义 TableViewCell 或您想要添加底线的任何视图中使用它。

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    self.addSeparator(ofHeight: 1)
}

【讨论】:

    【解决方案2】:
    private let kSeparatorId = 123
    private let kSeparatorHeight: CGFloat = 1.5
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
    {
        if cell.viewWithTag(kSeparatorId) == nil //add separator only once
        {
            let separatorView = UIView(frame: CGRectMake(0, cell.frame.height - kSeparatorHeight, cell.frame.width, kSeparatorHeight))
            separatorView.tag = kSeparatorId
            separatorView.backgroundColor = UIColor.redColor()
            separatorView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    
            cell.addSubview(separatorView)
        }
    }
    

    【讨论】:

    • 我的单元格可以是不同的高度,这会在用户滚动时导致这个出色的解决方案出现问题。为了解决这个问题,我用以下代码替换了 if 语句: cell.viewWithTag(kSeparatorId)?.removeFromSuperview()
    【解决方案3】:

    将表格 separatorsType 设置为 UITableViewCellSeparatorStyleNone 并配置一个带有 bg 颜色的单元格 backroundView 作为分隔符和一个子视图,用于尽可能多地屏蔽它。 像这样的:

    UIView * bg = [[UIView alloc] initWithFrame:cell.bounds];
    bg.backgroundColor = [UIColor darkGrayColor];
    bg.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    UIView * overBg = [[UIView alloc] initWithFrame:CGRectInset(cell.bounds, 0, 4.)];
    overBg.backgroundColor = [UIColor whiteColor];
    overBg.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    [bg addSubview:overBg];
    cell.backgroundView = bg;
    

    【讨论】:

      【解决方案4】:

      这样做的唯一方法是将separtorStype 设置为UITableViewCellSeparatorStyleNone,然后您有两个选择:

      • 创建一个自定义UITableViewCell,其中包含分隔符或
      • 使用您想要的分隔符创建一个备用UITableViewCell,并将其放置在每个其他单元格中。如果您想在表格上显示 3 行,则应在第 2 行和第 4 行中显示 5 并替换单元格。

      【讨论】:

      • 或者...您可以在添加单元格后添加一个空白单元格(具有固定高度)。
      • 这个答案是正确的并且工作正常,在这个链接中你可以找到代码示例stackoverflow.com/questions/39571799/…
      猜你喜欢
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      • 2011-07-10
      • 1970-01-01
      相关资源
      最近更新 更多