Swift UITableView 分隔符隐藏直到滚动

Swift UITableView separator hidden until scroll

我实现了一个自定义 table 视图单元格,它出现但没有分隔符,直到您滚动。在 viewDidAppear 中,我设置了分隔符样式,标签边框不与单元格边缘重叠。帮助

滚动前

滚动后

更大的 Sim Window

在设备测试中

模式是带有自定义单元格的 MVVM。

型号

import Foundation

struct OAuthList {

    let providers: [String]

    init() {
        self.providers = OAuthProviders.providers
    }
}

查看模型

import Foundation

struct OAuthListViewModel {

    var providerList: [String]

    init(providers: [String]) {

        self.providerList = providers
    }
}

登录视图控制器

import UIKit

class LoginViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var tableView: UITableView!

    var providerButtons = OAuthListViewModel(providers: OAuthProviders.providers)

    override func viewDidLoad() {
        super.viewDidLoad()

        let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 17)]
        self.navigationController?.navigationBar.titleTextAttributes = attributes
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.barTintColor = #colorLiteral(red: 1, green: 0.738589704, blue: 0.9438112974, alpha: 1)
        self.navigationItem.title = "LOGIN / SIGNUP"
        self.navigationItem.leftBarButtonItem?.tintColor = .white
        self.navigationItem.leftBarButtonItem?.isEnabled = false
        self.tableView.separatorColor = .white
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(CustomCell.self, forCellReuseIdentifier: TextCellIdentifier.textCellIdentifier)
        self.tableView.layoutMargins = UIEdgeInsets.zero
        self.tableView.separatorInset = UIEdgeInsets.zero
        self.tableView.tableFooterView = UIView()
    }
}

extension LoginViewController {

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return providerButtons.providerList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: TextCellIdentifier.textCellIdentifier, for: indexPath) as! CustomCell

        let row = indexPath.row
        cell.backgroundColor = #colorLiteral(red: 1, green: 0.738589704, blue: 0.9438112974, alpha: 1)
        cell.buttonLabel.text = providerButtons.providerList[row]
        cell.layoutMargins = UIEdgeInsets.zero
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        print(providerButtons.providerList[indexPath.row])

    }
}

自定义单元格

class CustomCell: UITableViewCell {

    var labelText: String?

    var buttonLabel: UILabel = {
        var label = UILabel()
        return label
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: TextCellIdentifier.textCellIdentifier)
        self.addSubview(buttonLabel)
        buttonLabel.translatesAutoresizingMaskIntoConstraints = false

        buttonLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        buttonLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        buttonLabel.textColor = UIColor.white
    }

    override func layoutSubviews() {

        if let labelText = labelText {
            buttonLabel.text = labelText
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

您可以隐藏分隔符,只需添加 UIView 作为自定义单元格中的分隔符

扩大您的模拟器 window 或在非 sim 的实际设备上尝试。

当 sim window 足够小时,它可能很难在表格视图中显示分隔符,因为分隔符通常只有 0.5pt 的高度。

当您在模拟器上滚动表格视图时,一个很好的迹象表明我上面提到的问题正在发生,随机分隔符开始出现,而有些分隔符被隐藏。这就是您的第二张屏幕截图中发生的情况。