编程UITableView仅显示一个单元格

编程UITableView仅显示一个单元格,uitableview,swift3,custom-cell,Uitableview,Swift3,Custom Cell,一个敏捷的新手。我试图学习如何以编程方式创建不同的UI元素。我撞到了下面的墙 我有2个swift文件,一方面我们有 import UIKit struct MyTableView { let myCustomTable: UITableView = { let aTable = UITableView() aTable.register(MyCustomCell.self, forCellReuseIdentifier: "myCell") return

一个敏捷的新手。我试图学习如何以编程方式创建不同的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 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
    }()
}

我知道它会像一个括号或一个点一样微不足道。谢谢你的帮助。已经试了两个小时了!