自定义TableViewCell中的TableView空单元格 - Swift

3
我目前正在学习以编程方式使用Swift。我想在一个viewController中添加一个tableView(为了能够稍后操作约束)并使用TableViewCell自定义单元格。
在使用storyboard时,我可以闭着眼睛做到这一点,但是当我尝试使用纯粹的代码时,单元格为空。
我的storyboard由一个空的viewController组成,其自定义类为ViewController。
我已经查看了其他类似问题的解决方案,但没有一个解决方案起作用。很想知道我忽略了什么(可能很简单)。提前感谢您的帮助!
ViewController.swift:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var tableView: UITableView = UITableView()
var items: [String] = ["Viper", "X", "Games"]

override func viewDidLoad() {

    tableView.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
    tableView.delegate = self
    tableView.dataSource = self

    tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "cell")

    self.view.addSubview(tableView)
}

func tableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
    return 50
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.items.count
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell

    //cell.textLabel?.text = self.items[indexPath.row]
    cell.companyName.text = "name"

    return cell
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

TableViewCell:

 import UIKit

class TableViewCell: UITableViewCell {

var companyName = UILabel()

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    companyName.frame = CGRectMake(0, 0, 200, 20)

    companyName.translatesAutoresizingMaskIntoConstraints = false

    contentView.addSubview(companyName)
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}
4个回答

4

你好 @Michael,首先,当你使用.xib(Nib)文件时,你应该只使用awakeFromNib方法。在你的情况下,你正在使用自定义类而没有这样的xib文件,因此,你应该使用:

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

    companyName = UILabel(frame: CGRectMake(0, 0, 200, 20))
    contentView.addSubview(companyName)

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

在使用标签之前,您需要初始化它。这将解决您的问题。

阅读苹果的文件以了解如何子类化UITableViewCell 在此处


谢谢!这是在@replman的指引下,我找到的答案。你说的“在使用之前先初始化标签”是什么意思?它已经被初始化了。 - Geppelt
你刚刚设置了UILabel的框架,由于你没有为单元格创建xib,最好在将其添加到任何视图之前先这样做,这样你就不会得到nil对象。此外,你应该在cell类中实现prepareForReuse方法,在下一次使用对象之前进行清理。 - Bhaumik Desai
我不想要一个xib文件,也不明白你的意思;UIImageView已经被声明、框架化和添加了。我没有遇到nil对象的问题。 - Geppelt

1
如果您想让自定义单元格从某个自定义xib加载,有时会这样做:
tableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "CustomTableViewCell")

您应该拥有一个名为CustomTableViewCell.xib的文件,其中包含具有重用标识符CustomTableViewCell的表视图单元格。


是的,但正如我所说,我是在程序上实现的。因此,我已经注册了该类。 - Geppelt
帮了我很大的忙,谢谢。关于这种方法的文章:https://medium.com/@musawiralishah/creating-custom-uitableviewcell-using-nib-xib-files-in-xcode-9bee5824e722 - Pavel Shorokhov

0

检查一下你的单元格中的companyLabel是如何布局的。它是否存在?

在你的代码中,我用默认的textLabel替换了companyLabel,这对我起作用了。

cell.textLabel!.text = self.items[indexPath.row]

这是单元格的“默认”textLabel(居中对齐Y)。我的标签“companyName”位于0,0,因此如果它出现了,它将被藏在左上角。我正在尝试弄清楚这一点,以便向TableViewCell添加更多项目。 - Geppelt
这里的想法是-你的表格视图没有任何问题。如果默认的单元格文本标签出现,那么你需要调试的区域就缩小到自定义单元格。祝你好运! - Abhinav

0

我认为awakeFromNib没有被调用是因为你注册的是一个类而不是一个nib。尝试使用以下代码:

class TableViewCell: UITableViewCell {
    let companyName = UILabel()

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

        // Initialization code
        companyName.frame = CGRectMake(0, 0, 200, 20)
        companyName.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(companyName)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

那会是什么样子? - Geppelt
类似这样的代码: override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) } - Geppelt
@MichaelDavidGeppelt,你为什么把我回答的标志移除了? - Florian L.
你之前提供的示例让我找到了解决方案,这也是我最初标记它的原因。但是,你提供的示例代码仍然会产生空单元格。你带我找到的解决方案更像是当前被标记的答案。@replman - Geppelt
@MichaelDavidGeppelt 如果想要快速简化代码,就会发生这种情况 :-)我修改了我的答案,只是为了反映我最初的评论。 - Florian L.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接