编程 UITableView 仅显示一个单元格

Programmatic UITableView is showing one cell only

Swift 新手。我正在尝试学习如何以编程方式创建不同的 UI 元素。我撞到了下面的墙..

我有 2 个 .swift 文件,一方面我们有...

import UIKit

struct MyTableView {

let myCustomTable: UITableView = {

    let aTable = UITableView()
        aTable.register(MyCustomCell.self, forCellReuseIdentifier: "myCell")

    return aTable
    }()

}

//  custom cell class, then add subviews (UI controls) to it

class MyCustomCell: UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    addSubview(aLabel)
    aLabel.frame = CGRect(x:0,
                          y:0,
                          width:self.frame.width,
                          height:self.frame.height)
}

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

}

//  stuff to add to the cell

let aLabel: UILabel = {

    let lbl = UILabel()
        lbl.text = "My Custom Cell"
        lbl.backgroundColor = UIColor.yellow   // just to highlight it on the screen

    return lbl
}()

另一方面,我们有以下视图控制器...

import UIKit

    class ViewControllerA: UIViewController, UITableViewDelegate, UITableViewDataSource {

        private let instanceOfViewControllerTable = MyTableView()

        override func loadView() {

            super.loadView()

                view.frame = UIScreen.main.bounds

            instanceOfViewControllerTable.myCustomTable.delegate = self
            instanceOfViewControllerTable.myCustomTable.dataSource = self

            instanceOfViewControllerTable.myCustomTable.frame = CGRect(x:0,
                                                                       y:0,
                                                                       width:self.view.frame.width,
                                                                       height:self.view.frame.height)

            self.view.addSubview(instanceOfViewControllerTable.myCustomTable)


        }


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

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

            return tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)

        }

    }

它构建并成功运行,但是,我得到以下结果:

现在,我的想法是,如果我做错了什么,单元格根本不应该出现。我不明白的是,为什么它只显示在数组中的一个单元格上?

非常感谢您的帮助。

您正在将 aLabel 声明为 全局变量 。这样一来,它只存在一个实例。将它移到单元格的 class 声明中。

class MyCustomCell: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        addSubview(aLabel)
        aLabel.frame = CGRect(x:0,
                              y:0,
                              width:self.frame.width,
                              height:self.frame.height)
    }

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

    let aLabel: UILabel = {
        let lbl = UILabel()
        lbl.text = "My Custom Cell"
        lbl.backgroundColor = UIColor.yellow   // just to highlight it on the screen

        return lbl
    }()
}