Swift-tableView的顶部为背景色

时间:2018-08-24 18:22:27

标签: ios swift uitableview swift4

我真的不知道如何描述我的问题,所以这是一个视频:

enter image description here

我有一个play.mailer { smtp.host = "smtp.gmail.com" // (mandatory) port = 465// (defaults to 25) ssl = true // (defaults to no) tls = false // (defaults to no) tlsRequired =false // (defaults to no) user = "xxxxxx@gmail.com" // (optional) password = "12121212" // (optional) // debug = false // (defaults to no, to take effect you also need to set the log level to "DEBUG" for the application logger) timeout = 600 // (defaults to 60s in milliseconds) connectiontimeout = 600 // (defaults to 60s in milliseconds) mock = true // (defaults to no, will only log all the email properties instead of sending an email) } ,我希望橙色的顶部是红色,底部是白色。 我试图更改背景颜色(橙色),这不是我想要的...

您知道是否有可能这样做,因为有时红色部分的文字可能会很短,我们可以看到tableViewController

的顶部和底部

2 个答案:

答案 0 :(得分:2)

将红色视图添加到表视图的同一父视图中,并将位置设置为表视图的顶部。其宽度应为tablview视图的宽度,而高度最初应为0。使用滚动视图委托并将红色视图的高度设置为max(-scrollView.contentOffset.y,0)。将表格视图的背景色设置为白色。

答案 1 :(得分:1)

这是代码示例。为您的视图控制器进行更改。

import UIKit

class ViewController: UIViewController {

// Count your number of cells from your array. 3 its just example.
private let numberOfRedCells: CGFloat = 3
private let cellHeight: CGFloat = 50

var defaultOffSet: CGPoint?
var defaultHeightConstraint: NSLayoutConstraint!

lazy var tableView: UITableView = {
    let tableView  = UITableView(frame: self.view.frame)
    // Important. Clear backgroundColor.
    tableView.backgroundColor = .clear
    tableView.separatorStyle = .none
    tableView.dataSource = self
    tableView.delegate = self
    return tableView
}()

lazy var redView: UIView = {
    let rv = UIView()
    rv.backgroundColor = .red
    return rv
}()

override func viewDidLoad() {
    super.viewDidLoad()


    tableView.register(UITableViewCell.self, forCellReuseIdentifier: NSStringFromClass(UITableViewCell.self))

    view.addSubview(redView)
    setupConstraints()
    view.addSubview(tableView)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    defaultOffSet = tableView.contentOffset
}

private func setupConstraints() {
    redView.translatesAutoresizingMaskIntoConstraints = false
    let top = NSLayoutConstraint(item: redView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0)
    let left = NSLayoutConstraint(item: redView, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1, constant: 0)
    let right = NSLayoutConstraint(item: redView, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1, constant: 0)
    let bottom = NSLayoutConstraint(item: redView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0)
    view.addConstraints([top, left, right, bottom])
}

}

extension ViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 8
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(UITableViewCell.self), for: indexPath)
    // Your red type cells, equals 3 just for example
    if indexPath.row < 3 {
        cell.backgroundColor = .red
        cell.textLabel?.text = "Input your text here"
    } else {
        // Your white type cells
        cell.backgroundColor = .white
        cell.textLabel?.text = "Your information"
    }

    return cell
}

}

extension ViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return cellHeight
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let contentYoffset = scrollView.contentOffset.y + 20 // 20 - statusBar height

    if contentYoffset > 0 {
        self.redView.frame.size.height = 20
    } else {
        self.redView.frame.size.height = cellHeight * numberOfRedCells - contentYoffset
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}

}

这就是我所拥有的。 enter link description here