【问题标题】:Custom TableViewCells do not show up自定义 TableView 单元格不显示
【发布时间】:2019-02-22 17:32:01
【问题描述】:

所以我对 iOS 开发还很陌生。我尝试以编程方式创建所有内容,因此我的故事板是空的。我目前正在尝试使用自定义单元格获取 TableView。当我使用标准 UITableViewCell 时,TableView 正在运行并且看起来很好。我创建了一个非常简单的类,名为“GameCell”。基本上,我想在这里创建一个带有多个标签的单元格,将来可能还有一些额外的 UIObjects(imageView 等)。由于某种原因,自定义单元格不显示。

游戏单元类:

class GameCell: UITableViewCell {

    var mainTextLabel = UILabel()
    var sideTextLabel = UILabel()

    func setLabel() {
        self.mainTextLabel.text = "FirstLabel"
        self.sideTextLabel.text = "SecondLabel"
    }
}

这里是获取行数并将单元格返回到我在 ViewController 中的 TableView 的额外必要代码。 self.lastGamesCount 在这里只是一个 Int,当我打印它时绝对不为零。

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.lastGamesCount
        }

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

let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as! GameCell

在我的 viewDidLoad() 中,我像这样注册单元格:

tableView.register(GameCell.self, forCellReuseIdentifier: cellID)

当我运行构建成功的所有内容时,我可以看到我的应用程序的导航栏,除了 TableView 之外的所有内容都是空的。我回到正常的 UITableViewCell 并且单元格再次出现。我在这里想念什么?任何帮助表示赞赏。

谢谢!

【问题讨论】:

  • 您在哪里尝试将标签添加到单元格并设置它们的框架?

标签: ios swift uitableview


【解决方案1】:

问题是你需要为这些标签设置约束

var mainTextLabel = UILabel()
var sideTextLabel = UILabel()

将它们添加到单元格后

class GameCell: UITableViewCell {

    let mainTextLabel = UILabel()
    let sideTextLabel = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setLabel()
    }
    func setLabel() { 
        self.mainTextLabel.translatesAutoresizingMaskIntoConstraints = false
        self.sideTextLabel.translatesAutoresizingMaskIntoConstraints = false 
        self.contentView.addSubview(mainTextLabel)
        self.contentView.addSubview(sideTextLabel) 
        NSLayoutConstraint.activate([  
            mainTextLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor), 
            mainTextLabel.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor), 
            mainTextLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor,constant:20), 
            sideTextLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor), 
            sideTextLabel.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor), 
            sideTextLabel.topAnchor.constraint(equalTo: self.mainTextLabel.bottomAnchor,constant:20), 
            sideTextLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor,constant:-20)
        ])
        self.mainTextLabel.text = "FirstLabel"
        self.sideTextLabel.text = "SecondLabel"
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    } 
}

【讨论】:

  • 谢谢!由于我没有太多经验并且基本上刚刚开始,我想知道这是否是常见的方法。 Internet 上的大多数教程都使用 Main Storyboard,但我认为在以编程方式进行诸如添加对象等操作时学习体验可能会更好。这里的一般方法是什么?
  • @JDK92 你问的是代码还是故事板?这取决于项目及其背后的人。所有方法都有其缺点。以编程方式进行是大团队的首选方式。解决代码中的合并冲突要容易得多。
猜你喜欢
  • 2019-08-21
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
相关资源
最近更新 更多