自定义TableViewCell没有故事板

我正在使用从TableViewCell扩展自定义cocoa类,它不会给出任何错误消息,但单元格不会出现在tableview中。 滚动变长,但表格单元格不可见。

我在我的ViewController中键入这个:

tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "Cell") func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)->UITableViewCell { var cell:CustomTableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? CustomTableViewCell if cell == nil { cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") } cell!.labTime.text = filtered[indexPath.row].name! return cell! } 

和我的单元格类似

  var labTime = UILabel() override func awakeFromNib() { // Initialization code super.awakeFromNib() labTime = UILabel(frame: contentView.bounds) labTime.font = UIFont(name:"HelveticaNeue-Bold", size: 16) contentView.addSubview(labTime) } 

我不使用任何storyBoard或xib文件。 找不到解决方法,谢谢

这样做。

所有的属性初始化应该在init方法中进行。 将其添加到您的自定义单元类中

 var labTime: UILabel! required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init(style: UITableViewCellStyle, reuseIdentifier: String!) { super.init(style: style, reuseIdentifier: reuseIdentifier) //Do your cell set up labTime = UILabel(frame: contentView.bounds) labTime.font = UIFont(name:"HelveticaNeue-Bold", size: 16) contentView.addSubview(labTime) } 

在视图控制器的viewDidLoad方法中添加以下行。

 tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "Cell") 

设置这两个委托 – UITableViewDataSourceUITableViewDelegate