简体   繁体   English

自定义 tableView 根本不显示

[英]Custom tableView not showing at all

I'm trying to program a custom table view and I'm having difficulties getting it to show at all.我正在尝试编写自定义表格视图,但根本无法显示它。 The rootViewController is set to HoursTableViewController. rootViewController 设置为HoursTableViewController。 I tried changing the background color of the tableView to see if it would show, but it doesn't.我尝试更改 tableView 的背景颜色以查看它是否会显示,但它不会。 I'm pretty lost at this point.在这一点上我很迷茫。 I also get this error in the console but I'm not sure if this is relevant to my problem:我也在控制台中收到此错误,但我不确定这是否与我的问题有关:

Unknown class _TtC12Voluntracker14ViewController in Interface Builder file. Interface Builder 文件中的未知类 _TtC12Voluntracker14ViewController。

Any help would be greatly appreciated!任何帮助将不胜感激! I tried to change attributes of the tableView suggested in other posts but it still doesn't show.我试图更改其他帖子中建议的 tableView 的属性,但它仍然没有显示。

    class HoursTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var hourDonationSegmentedButton: UISegmentedControl!
    var addEntryButton: UIButton!
    var editEntryButton: UIButton!

    var hoursEntries = [String]()
    var hoursTableView: UITableView = HoursTableView()




    override func viewDidLoad() {
        super.viewDidLoad()
        hoursTableView.dataSource = self
        hoursTableView.delegate = self
        hoursTableView.register(HoursTableViewCell.self, forCellReuseIdentifier: "HoursTableViewCell")
        prepareTableView()
    }

    func prepareTableView() {
        view.addSubview(hoursTableView)
        hoursTableView.backgroundColor = .green
        hoursTableView.translatesAutoresizingMaskIntoConstraints = false
        hoursTableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        hoursTableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        hoursTableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        hoursTableView.rowHeight = UITableView.automaticDimension
        hoursTableView.estimatedRowHeight = 44
        hoursTableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        hoursTableView.backgroundColor = .green
    }


}

extension HoursTableViewController {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1;

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = hoursTableView.dequeueReusableCell(withIdentifier: "HoursTableViewCell", for: indexPath) as! HoursTableViewCell
        //let hoursEntry = hoursEntries[indexPath.row]
        cell.hourLabel.text = "6"
        cell.minuteLabel.text = "6"
        cell.dateLabel.text = "6"
        cell.organizationLabel.text = "6"
        //cell = makeCellFromHoursEntry(currentCell: cell, hoursEntry: hoursEntry)
        return cell
    }

Putting it as an answer as it's becoming too long for a comment.将其作为答案,因为评论时间太长了。

Are constraints asking to autolayout and manual sizing specifying the exact size?约束是否要求自动布局和手动调整大小指定确切的大小? answer is Yes .答案是肯定的

If you want to set the frame just remove translateAutoResizingMaskIntoConstraint or set it to translatesAutoresizingMaskIntoConstraints = true .如果您想设置框架,只需删除translateAutoResizingMaskIntoConstraint或将其设置为translatesAutoresizingMaskIntoConstraints = true Problem there will be your anchors will not be taken into consideration during layout.问题是在布局期间不会考虑您的锚点。

if you want it to be fully autoLayout remove the frame and just set the bottom constraint as hoursTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true如果您希望它完全自动布局,请删除框架并将底部约束设置为hoursTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM