我在尝试以编程方式创建动态原型单元时遇到问题。这是我第一次尝试这种方法。我总是通过故事板创建它们。
我的ViewController.swift文件包含以下内容
var noteListTableView: UITableView = UITableView()
noteListTableView.frame.origin.x = 0.0
noteListTableView.frame.origin.y = kNAV_BAR_HEIGHT
noteListTableView.frame.size.width = kDEVICE_WIDTH
noteListTableView.frame.size.height = kDEVICE_HEIGHT - kNAV_BAR_HEIGHT
noteListTableView.dataSource = self
noteListTableView.delegate = self
self.view.addSubview(noteListTableView)
同样在文件中,我已经包含了相应的TableView委托和数据源函数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return GlobalDataController.notesDB.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var aNote: NoteItem = GlobalDataController.notesDB[indexPath.row] as NoteItem
if var cell: NoteListTVCell = noteListTableView.dequeueReusableCellWithIdentifier(kReuseCellNoteList) as? NoteListTVCell {
cell.noteTitle.text = "Title"
cell.noteContentSummary.text = "Content Summary"
cell.noteDate.text = "May 20"
return cell
}
else{
var cell: NoteListTVCell = NoteListTVCell(style: UITableViewCellStyle.Default, reuseIdentifier: kReuseCellNoteList)
cell.noteTitle.text = "Title"
cell.noteContentSummary.text = "Content Summary"
cell.noteDate.text = "May 20"
return cell
}
}
(1)IF-STATEMENT似乎总是失败,这就是为什么我按照我在网上为Obj-C找到的一些例子添加了else语句。
(2)当ELSE被触发时,程序总是崩溃,试图分配单元格。*值,说它遇到了零值。
任何想法?
编辑:NoteListTVCell class NoteListTVell:UITableViewCell {
var noteTitle: UILabel!
var noteContentSummary: UILabel!
var noteDate: UILabel!
var cellMargin: CGFloat = kPadding * 3
override func awakeFromNib() {
super.awakeFromNib()
noteTitle = UILabel(frame: CGRect(x: cellMargin, y: kPadding, width: kDEVICE_WIDTH - cellMargin - kPadding, height: 30.0))
noteTitle.backgroundColor = kColorTransparent
noteTitle.font = UIFont(name: kFont02, size: 20.0)
noteTitle.textColor = Scripts.hexColor(0x000000, alpha: 0.8)
noteTitle.text = "Title"
self.addSubview(noteTitle)
/* Note Content Summary
-----------------------------------------------------------------------------------------*/
var noteContentSummaryY: CGFloat = noteTitle.frame.origin.y + noteTitle.frame.size.height
noteContentSummary = UILabel(frame: CGRect(x: cellMargin, y: noteContentSummaryY, width: kDEVICE_WIDTH - cellMargin - kPadding, height: 20.0))
noteContentSummary.backgroundColor = kColorTransparent
noteContentSummary.font = UIFont(name: kFont02, size: 14.0)
noteContentSummary.textColor = Scripts.hexColor(0x000000, alpha: 0.4)
noteContentSummary.text = "This is a summary..."
self.addSubview(noteContentSummary)
/* Note Date
-----------------------------------------------------------------------------------------*/
noteDate = UILabel(frame: CGRect(x: kDEVICE_WIDTH - 100.0 - kPaddingx2, y: kPadding, width: 100.0, height: 20.0))
noteDate.backgroundColor = kColorTransparent
noteDate.font = UIFont(name: kFont02, size: 10.0)
noteDate.textColor = Scripts.hexColor(kColorThemeBlue, alpha: 1.0)
noteDate.textAlignment = NSTextAlignment.Right
noteDate.text = "May 8, 2:14 AM"
self.addSubview(noteDate)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
答案 0 :(得分:1)
您需要在viewDidLoad
注册自定义tableViewCell类:
notelistTableView.registerClass(NoteListTVCell.self, forCellReuseIdentifier: kReuseCellNoteList)
在InterfaceBuilder中设计原型时,这是自动完成的。在代码中设计单元格时需要它。
答案 1 :(得分:1)
您没有覆盖自定义单元格中的init方法,但无论如何,如果您不提供,它将调用super init方法。将您的单元初始化代码放在init方法中而不是awakeFromNib方法。
请尝试以下操作。
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
noteTitle = UILabel(frame: CGRect(x: cellMargin, y: kPadding, width: kDEVICE_WIDTH - cellMargin - kPadding, height: 30.0))
noteTitle.backgroundColor = kColorTransparent
noteTitle.font = UIFont(name: kFont02, size: 20.0)
noteTitle.textColor = Scripts.hexColor(0x000000, alpha: 0.8)
noteTitle.text = "Title"
self.addSubview(noteTitle)
/* Note Content Summary
-----------------------------------------------------------------------------------------*/
var noteContentSummaryY: CGFloat = noteTitle.frame.origin.y + noteTitle.frame.size.height
noteContentSummary = UILabel(frame: CGRect(x: cellMargin, y: noteContentSummaryY, width: kDEVICE_WIDTH - cellMargin - kPadding, height: 20.0))
noteContentSummary.backgroundColor = kColorTransparent
noteContentSummary.font = UIFont(name: kFont02, size: 14.0)
noteContentSummary.textColor = Scripts.hexColor(0x000000, alpha: 0.4)
noteContentSummary.text = "This is a summary..."
self.addSubview(noteContentSummary)
/* Note Date
-----------------------------------------------------------------------------------------*/
noteDate = UILabel(frame: CGRect(x: kDEVICE_WIDTH - 100.0 - kPaddingx2, y: kPadding, width: 100.0, height: 20.0))
noteDate.backgroundColor = kColorTransparent
noteDate.font = UIFont(name: kFont02, size: 10.0)
noteDate.textColor = Scripts.hexColor(kColorThemeBlue, alpha: 1.0)
noteDate.textAlignment = NSTextAlignment.Right
noteDate.text = "May 8, 2:14 AM"
self.addSubview(noteDate)
}
在viewDidLoad
中执行此操作override func viewDidLoad() {
super.viewDidLoad()
noteListTableView = UITableView(frame: self.view.frame, style: UITableViewStyle.Plain)
noteListTableView.dataSource = self
noteListTableView.delegate = self
self.view.addSubview(noteListTableView)
noteListTableView.registerClass(NoteListTVCellTableViewCell.self, forCellReuseIdentifier: kReuseCellNoteList)
// Do any additional setup after loading the view, typically from a nib.
}