我試圖在自定義視圖中放置一個 uitableview,但表格視圖占據了整個視圖,并且似乎所有邊約束都為零,即使我從界面構建器中給它約束。而且自定義單元筆尖也沒有出現!
這是我的自定義視圖類代碼:
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合適的視圖

這是我得到的結果

最后是表格視圖單元格類
最后這是我合適的視圖單元類
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
}
}
uj5u.com熱心網友回復:
如果我理解得很好,您可以以編程方式進行。在您的控制器類中宣告 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")
}
}
結果如下:灰色空間是控制器背景,紅色空間是表格視圖的容器,橙色是表格視圖

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/427941.html
