Ios 如何以编程方式使用UITableViewCell的子类生成UITableView?
这里没有好的答案。所以我想知道如何制作这个。 例如,在UITableViewCell的customClass中,我想制作一个带有照片和标签的原型单元Ios 如何以编程方式使用UITableViewCell的子类生成UITableView?,ios,swift,uitableview,Ios,Swift,Uitableview,这里没有好的答案。所以我想知道如何制作这个。 例如,在UITableViewCell的customClass中,我想制作一个带有照片和标签的原型单元 class myCell: UITableViewCell { init(style: UITableViewCellStyle, reuseIdentifier: String?){ } required init?(coder aDecoder: NSCoder) { super.init(cod
class myCell: UITableViewCell {
init(style: UITableViewCellStyle, reuseIdentifier: String?){
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
....
}
class myController: UIViewController {
....
}
我想你们先在myController注册myCell然后
the cell = let cell = tableView.dequeueReusableCell() so on.
如果我在myController中注册myCell,则无需使用
init(style: UITableViewCellStyle, reuseIdentifier: String?){
}
。如果我不使用以下命令,如何将子视图添加到UITableViewCell视图
init(style: UITableViewCellStyle, reuseIdentifier: String?){
}
注:我想注册传感器,因为我只需要一次称重传感器。并使用dequeueReusableCell()
你能告诉我如何专业地做这件事吗?
你可以用你自己的方法。我只想要一个有照片和标签的原型电池。所有进程都以编程方式进行。您可以定义自己的表视图单元格类:
class CustomCell : UITableViewCell {
static let identifier = "CustomCell"
// properties, etc., e.g.:
private let label = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// here you do the setup, adding subviews, etc., e.g.:
self.contentView.addSubview(label)
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 4),
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -4),
label.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 8),
label.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -8),
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//other methods, e.g., for configuring the cell, e.g.:
func configure(withTitle title: String) {
label.text = title
}
}
然后您只需在CustomTableController
中使用它:
class CustomTableController: UITableViewController {
fileprivate var data: [Model] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
// registering a cell class for reuse is important
// but it's enough to make it once in viewDidLoad
tableView.register(CustomCell.self, forCellReuseIdentifier: CustomCell.identifier)
tableView.reloadData()
}
// MARK: Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// here you can dequeue your cell
let cell = tableView.dequeueReusableCell(withIdentifier: CustomCell.identifier, for: indexPath) as! CustomCell
// configure the cell using model, e.g.:
cell.configure(withTitle: data[indexPath.row].title)
return cell
}
}
您可以定义自己的表视图单元类:
class CustomCell : UITableViewCell {
static let identifier = "CustomCell"
// properties, etc., e.g.:
private let label = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// here you do the setup, adding subviews, etc., e.g.:
self.contentView.addSubview(label)
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 4),
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -4),
label.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 8),
label.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -8),
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//other methods, e.g., for configuring the cell, e.g.:
func configure(withTitle title: String) {
label.text = title
}
}
然后您只需在CustomTableController
中使用它:
class CustomTableController: UITableViewController {
fileprivate var data: [Model] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
// registering a cell class for reuse is important
// but it's enough to make it once in viewDidLoad
tableView.register(CustomCell.self, forCellReuseIdentifier: CustomCell.identifier)
tableView.reloadData()
}
// MARK: Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// here you can dequeue your cell
let cell = tableView.dequeueReusableCell(withIdentifier: CustomCell.identifier, for: indexPath) as! CustomCell
// configure the cell using model, e.g.:
cell.configure(withTitle: data[indexPath.row].title)
return cell
}
}
这和我做的一样。我的问题是,我不会在任何地方调用init(style:UITableViewCellStyle,reuseIdentifier:String?)方法。那么它是如何将子视图添加到单元视图的呢?当我注册它的时候?我很困惑。@John当你调用
tableView.dequeueReusableCell
时,如果没有已经创建的单元格,tableView
将使用init(style:reuseIdentifier:)
初始值设定项实例化一个新的单元格-这就是为什么你必须调用super.init(style:style,reuseIdentifier:reuseIdentifier)
以及为什么我建议在特定的初始值设定项中设置单元格。。。在单元重用的情况下,您不会自己创建单元实例,而是将其留给表视图
,并使用dequeueReusableCell
methods@John我在答案中添加了一些例子,现在就看出来。非常感谢,我现在明白了一切。特别是为什么我们也使用super.init。还有一件事我想问你,因为你解释得很好。customCell.self为什么要添加self?这和我做的一样。我的问题是,我不会在任何地方调用init(style:UITableViewCellStyle,reuseIdentifier:String?)方法。那么它是如何将子视图添加到单元视图的呢?当我注册它的时候?我很困惑。@John当你调用tableView.dequeueReusableCell
时,如果没有已经创建的单元格,tableView
将使用init(style:reuseIdentifier:)
初始值设定项实例化一个新的单元格-这就是为什么你必须调用super.init(style:style,reuseIdentifier:reuseIdentifier)
以及为什么我建议在特定的初始值设定项中设置单元格。。。在单元重用的情况下,您不会自己创建单元实例,而是将其留给表视图
,并使用dequeueReusableCell
methods@John我在答案中添加了一些例子,现在就看出来。非常感谢,我现在明白了一切。特别是为什么我们也使用super.init。还有一件事我想问你,因为你解释得很好。customCell.self在这里为什么要添加self?