自定义 UIView 内的 UITableview
UITable view inside custom UIView
我试图在自定义视图中放置一个 uitable 视图,但是 table 视图占据了整个视图,即使我给出了所有边约束,它似乎也采用了零它受接口 builder 的约束。而且自定义单元格笔尖也没有出现!
这是我的自定义视图 class 代码:
import UIKit
class Example: UIView {
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var bitTableList: UITableView!
override func awakeFromNib() {
super.awakeFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
Bundle.main.loadNibNamed("ContentPlayingLeftView", owner: self, options: nil)
self.contentView.fixInView(self)
bitTableList.register(PlayingNowTableViewCell.self, forCellReuseIdentifier: "PLAYINGNOWCELL")
bitTableList.delegate = self
bitTableList.dataSource = self
}
}
extension Example: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: PlayingNowTableViewCell = self.bitTableList.dequeueReusableCell(withIdentifier: "PLAYINGNOWCELL") as! PlayingNowTableViewCell
return cell
}
}
任何人都可以帮助我解决这个问题吗?
这里是 mu suitable 带有约束的视图
这是我得到的结果
最后 table 视图单元格 class
最后是我的 suitable 视图单元格 class
import UIKit
class PlayingNowTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
如果我理解的很好,你可以通过编程来实现。在您的控制器中 class 声明 containerView 和 tableView 并设置约束:
class Aiuto: UIViewController {
let tableView = UITableView()
let cellId = "cellId" //cell identifier
let containerTableView: UIView = {
let v = UIView()
v.backgroundColor = .red
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .darkGray
tableView.delegate = self
tableView.dataSource = self
tableView.register(MyCell.self, forCellReuseIdentifier: cellId) // register your custom cell
tableView.backgroundColor = .orange
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerTableView)
containerTableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
containerTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20).isActive = true
containerTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
containerTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
containerTableView.addSubview(tableView)
tableView.fillSuperview(padding: .init(top: 20, left: 20, bottom: 20, right: 20))
tableView.topAnchor.constraint(equalTo: containerTableView.topAnchor, constant: 20).isActive = true
tableView.bottomAnchor.constraint(equalTo: containerTableView.bottomAnchor, constant: -20).isActive = true
tableView.trailingAnchor.constraint(equalTo: containerTableView.trailingAnchor, constant: -20).isActive = true
tableView.leadingAnchor.constraint(equalTo: containerTableView.leadingAnchor, constant: 20).isActive = true
}
}
之后创建 tableView Delegate 和 DataSource 的扩展:
extension Aiuto: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! MyCell //declare your custom cell
cell.exampleLabel.text = "This is index path: \(indexPath.row + 1)"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
50
}
}
现在添加您的自定义单元格(我放了一个简单的标签)
class MyCell: UITableViewCell {
let exampleLabel: UILabel = {
let label = UILabel()
label.backgroundColor = UIColor(white: 0, alpha: 0.3)
label.font = .systemFont(ofSize: 16, weight: .semibold)
label.textColor = .black
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.backgroundColor = .white
contentView.addSubview(exampleLabel)
exampleLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5).isActive = true
exampleLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
exampleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5).isActive = true
exampleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 5).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
这是结果:灰色 space 是控制器背景,红色 space 是 table 视图的容器,橙色是 table 视图
我试图在自定义视图中放置一个 uitable 视图,但是 table 视图占据了整个视图,即使我给出了所有边约束,它似乎也采用了零它受接口 builder 的约束。而且自定义单元格笔尖也没有出现!
这是我的自定义视图 class 代码:
import UIKit
class Example: UIView {
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var bitTableList: UITableView!
override func awakeFromNib() {
super.awakeFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
Bundle.main.loadNibNamed("ContentPlayingLeftView", owner: self, options: nil)
self.contentView.fixInView(self)
bitTableList.register(PlayingNowTableViewCell.self, forCellReuseIdentifier: "PLAYINGNOWCELL")
bitTableList.delegate = self
bitTableList.dataSource = self
}
}
extension Example: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: PlayingNowTableViewCell = self.bitTableList.dequeueReusableCell(withIdentifier: "PLAYINGNOWCELL") as! PlayingNowTableViewCell
return cell
}
}
任何人都可以帮助我解决这个问题吗?
这里是 mu suitable 带有约束的视图
这是我得到的结果
最后 table 视图单元格 class
最后是我的 suitable 视图单元格 class
import UIKit
class PlayingNowTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
如果我理解的很好,你可以通过编程来实现。在您的控制器中 class 声明 containerView 和 tableView 并设置约束:
class Aiuto: UIViewController {
let tableView = UITableView()
let cellId = "cellId" //cell identifier
let containerTableView: UIView = {
let v = UIView()
v.backgroundColor = .red
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .darkGray
tableView.delegate = self
tableView.dataSource = self
tableView.register(MyCell.self, forCellReuseIdentifier: cellId) // register your custom cell
tableView.backgroundColor = .orange
tableView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerTableView)
containerTableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
containerTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20).isActive = true
containerTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
containerTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
containerTableView.addSubview(tableView)
tableView.fillSuperview(padding: .init(top: 20, left: 20, bottom: 20, right: 20))
tableView.topAnchor.constraint(equalTo: containerTableView.topAnchor, constant: 20).isActive = true
tableView.bottomAnchor.constraint(equalTo: containerTableView.bottomAnchor, constant: -20).isActive = true
tableView.trailingAnchor.constraint(equalTo: containerTableView.trailingAnchor, constant: -20).isActive = true
tableView.leadingAnchor.constraint(equalTo: containerTableView.leadingAnchor, constant: 20).isActive = true
}
}
之后创建 tableView Delegate 和 DataSource 的扩展:
extension Aiuto: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! MyCell //declare your custom cell
cell.exampleLabel.text = "This is index path: \(indexPath.row + 1)"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
50
}
}
现在添加您的自定义单元格(我放了一个简单的标签)
class MyCell: UITableViewCell {
let exampleLabel: UILabel = {
let label = UILabel()
label.backgroundColor = UIColor(white: 0, alpha: 0.3)
label.font = .systemFont(ofSize: 16, weight: .semibold)
label.textColor = .black
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.backgroundColor = .white
contentView.addSubview(exampleLabel)
exampleLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5).isActive = true
exampleLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
exampleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5).isActive = true
exampleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 5).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
这是结果:灰色 space 是控制器背景,红色 space 是 table 视图的容器,橙色是 table 视图