[英]Custom TableViewCell without storyboard
我正在使用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")
設置這兩個委托 - UITableViewDataSource
和UITableViewDelegate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.