我正在尝试创建自定义UITableViewCell
。这是代码:
class ImportedContactsTVCell: UITableViewCell {
var nameLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
var emailLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
var phoneLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
nameLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
emailLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
phoneLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.addSubview(nameLabel)
contentView.addSubview(emailLabel)
contentView.addSubview(phoneLabel)
var viewsDict = [
"nameLabel" : nameLabel,
"emailLabel" : emailLabel,
"phoneLabel" : phoneLabel
]
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[nameLabel]-[emailLabel]-[phoneLabel]-|", options: NSLayoutFormatOptions( 0 ), metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[nameLabel]-|", options: NSLayoutFormatOptions( 0 ), metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[emailLabel]-|", options: NSLayoutFormatOptions( 0 ), metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[phoneLabel]-|", options: NSLayoutFormatOptions( 0 ), metrics: nil, views: viewsDict))
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我正在使用它如下:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = ImportedContactsTVCell()
cell.nameLabel.text = "A"
cell.emailLabel.text = "B"
cell.phoneLabel.text = "C"
return cell
}
他们不是在故事板上创建的原型单元格。我错过了什么?
提前致谢。
答案 0 :(得分:1)
awakeFromNib
。您应该覆盖initWithStyle:reuseIdentifier:
,并将代码放在那里。您仍然可以使用ImportedContactsTVCell()
实例化单元格,因为您不需要任何样式选择。如果您使用的是多个单元格,则应该使用正常的出列机制来创建单元格。
答案 1 :(得分:-1)
您必须在故事板中拥有原型单元格。无法重复创建表格视图单元格
参考https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#specifying-a-custom-user-model