[英]Custom UITableViewCell without storyboard not displaying
UPDATE WITH ANSWER 用答案更新
I can use the textlabel property of the cell, i can set attributes to my custom labels and print the value (with the print after the switch), but i cannot get anything custom to display, not the background color, not the value, not anything. 我可以使用单元格的textlabel属性,可以将属性设置为我的自定义标签并打印值(在切换后使用打印),但是我无法自定义显示任何东西,不是背景色,不是值,不是任何东西。
Please note that i don't use storyboard at all for this, and the two classes are inside the same file (tried using two separate files, doesn't change anything so it is not related to my issue). 请注意,我根本不使用情节提要,并且这两个类都在同一个文件内(使用两个单独的文件进行了尝试,不更改任何内容,因此与我的问题无关)。
If anyone got an idea of what the issue is here and could point me to the right direction it would be great. 如果有人对这里存在的问题有所了解,并且可以指出正确的方向,那将是很好的。
class DayPickerTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
self.tableView.registerClass(DayPickerTableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.scrollEnabled = false
self.tableView.bounces = false
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 44
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 8
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! DayPickerTableViewCell
cell.textLabel!.text = "test"
switch indexPath.row {
case 0:
cell.dayModeLabel.text = "DAY"
break
case 1:
cell.dayModeLabel.text = "WEEK"
break
case 2:
cell.dayModeLabel.text = "WORK WEEK"
break
case 3:
cell.dayModeLabel.text = "WEEK-END"
break
case 4:
cell.dayModeLabel.text = "MONTH"
break
case 5:
cell.dayModeLabel.text = "YEAR"
break
case 6:
cell.dayModeLabel.text = "ALL HISTORY"
break
case 7:
cell.dayModeLabel.text = "CUSTOM"
break
default: break
}
print(cell.dayModeLabel.text)
return cell
}
}
class DayPickerTableViewCell: UITableViewCell {
var imgDayPicker = UIImageView()
var dayModeLabel = UILabel()
var dayDisplayLabel = UILabel()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
imgDayPicker.translatesAutoresizingMaskIntoConstraints = false
dayModeLabel.translatesAutoresizingMaskIntoConstraints = false
dayDisplayLabel.translatesAutoresizingMaskIntoConstraints = false
imgDayPicker.backgroundColor = UIColor.redColor()
dayModeLabel.backgroundColor = UIColor.blackColor()
dayDisplayLabel.backgroundColor = UIColor.greenColor()
self.contentView.addSubview(imgDayPicker)
self.contentView.addSubview(dayModeLabel)
self.contentView.addSubview(dayDisplayLabel)
let viewsDict = [
"image" : imgDayPicker,
"mode" : dayModeLabel,
"day" : dayDisplayLabel,
]
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[image(50)][mode(==day)]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
in order for the code to work, replace the awake from nib method with this : 为了使代码正常工作,请使用以下命令替换abake from nib方法:
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
imgDayPicker.translatesAutoresizingMaskIntoConstraints = false
dayModeLabel.translatesAutoresizingMaskIntoConstraints = false
dayDisplayLabel.translatesAutoresizingMaskIntoConstraints = false
imgDayPicker.backgroundColor = UIColor.redColor()
dayModeLabel.backgroundColor = UIColor.blackColor()
dayDisplayLabel.backgroundColor = UIColor.greenColor()
self.contentView.addSubview(imgDayPicker)
self.contentView.addSubview(dayModeLabel)
self.contentView.addSubview(dayDisplayLabel)
let viewsDict = [
"image" : imgDayPicker,
"mode" : dayModeLabel,
"day" : dayDisplayLabel,
]
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[image(50)][mode(==day)]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
The storyboard/xib loader sends awakeFromNib
. 故事板/ xib加载程序发送
awakeFromNib
。 Since you're not using a storyboard or xib, nothing is sending awakeFromNib
to the cell, so that code never runs. 既然你不使用故事板或XIB,没有什么是发送
awakeFromNib
到细胞内,使代码永远不会运行。
You could just call cell.awakeFromNib()
, although that would be misleading. 您可以只调用
cell.awakeFromNib()
,尽管那样会产生误导。 I would move the code into init
. 我会将代码移到
init
。
You also need more constraints on the subviews. 您还需要对子视图施加更多约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.