我正在使用自TableViewCell扩展的自定义可可类,它没有给出任何错误消息,但单元格未出现在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")
设置这两个委托-
UITableViewDataSource
和UITableViewDelegate
关于ios - 没有 Storyboard 的自定义UITableViewCell,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30489496/