没有故事板的自定义UITableViewCell不显示

时间:2015-11-25 16:08:02

标签: ios swift uitableview

更新答案

我可以使用单元格的textlabel属性,我可以将属性设置为我的自定义标签并打印值(使用打开后的打印),但我无法获得任何自定义显示,而不是背景颜色,而不是价值,而不是任何东西。

请注意,我根本不使用故事板,并且这两个类在同一个文件中(尝试使用两个单独的文件,不会改变任何内容,因此它与我的问题无关)。

如果有人知道这里的问题是什么,并且能指出正确的方向,那就太好了。

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
}

}

为了使代码工作,用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")
}

1 个答案:

答案 0 :(得分:1)

storyboard / xib加载程序发送awakeFromNib。由于您没有使用故事板或xib,因此没有任何内容将awakeFromNib发送到单元格,因此代码永远不会运行。

您可以致电cell.awakeFromNib(),但这会产生误导。我会将代码移到init

您还需要更多的子视图约束。