Swift UITableView分隔符一直隐藏直到滚动

问题描述 投票:0回答:2

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

滚动前

enter image description here

滚动后

enter image description here

较大的Sim窗口

enter image description here

在设备上测试

enter image description here

该模式是带有自定义单元的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
    }
}

LoginViewController

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")
    }
}
ios swift uitableview custom-cell
2个回答
0
投票

您可以隐藏分隔符,只需添加UIView即可在您的自定义单元格中充当分隔符


0
投票

放大模拟器窗口或在非SIM的实际设备上尝试。

当sim窗口足够小时,由于分隔符的高度通常仅为0.5pt,因此很难在表格视图中显示分隔符。

我上面提到的问题正在发生的一个很好的指示是,当您在模拟器上滚动表格视图时,当一些隐藏时,随机分隔符开始出现。这是第二个屏幕快照中正在发生的情况。

© www.soinside.com 2019 - 2024. All rights reserved.