gpt4 book ai didi

ios - 带有自定义单元格的 UITableView 为空

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

我实现了两个自定义单元格:

class TextFieldCell: UITableViewCell{
var label = UILabel()
var textField = UITextField()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
label.text = "Name"
textField.placeholder = "Enter Task Name Here"
addSubview(label)
addSubview(textField)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}

class SwitchCell : UITableViewCell {
var label = UILabel()
var switchControl = UISwitch()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
label.text = "State"
switchControl.on = true
addSubview(label)
addSubview(switchControl)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}

在我的 TableView 类中,我声明了这两个单元格:

let textFieldCell = TextFieldCell(style: .Default, reuseIdentifier: "textFieldCell")
let switchCell = SwitchCell(style: .Default, reuseIdentifier: "switchCell")

并在其中有委托(delegate)方法:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch indexPath.row {
case 0: return textFieldCell
case 1: return switchCell
default: return UITableViewCell()
}
}

但是当加载 TableView 时,我只看到第二行左侧的开关控件。我不知道为什么。

最佳答案

试试这个:

private let reuseIdentifier = "switchCell"
//identifier of your cell.

您似乎错过了注册单元格。

    let tableNib = UINib(nibName:"SwitchCell", bundle:nil)
self.tableView!.registerNib(tableNib,forCellReuseIdentifier:reuseIdentifier)

关于ios - 带有自定义单元格的 UITableView 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35480703/

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