gpt4 book ai didi

ios - 没有 Storyboard的自定义 UITableViewCell 不显示

转载 作者:行者123 更新时间:2023-11-28 06:54:55 25 4
gpt4 key购买 nike

更新答案

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

请注意,我根本不为此使用 Storyboard,并且这两个类在同一个文件中(尝试使用两个单独的文件,没有改变任何东西,所以它与我的问题无关)。

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

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
}

}

为了让代码工作,用这个替换 awake 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")
}

最佳答案

Storyboard/xib 加载器发送 awakeFromNib。由于您没有使用 Storyboard 或 xib,因此不会向单元格发送 awakeFromNib,因此代码永远不会运行。

您可以只调用 cell.awakeFromNib(),尽管这会产生误导。我会将代码移动到 init 中。

您还需要对 subview 进行更多约束。

关于ios - 没有 Storyboard的自定义 UITableViewCell 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33921220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com