所以基本上,我试图在ViewController中使用UITableView,它将引用其他UIView的视图。
所有视图都已显示,除了TableView之外,其他一切似乎都很好。我已经将tableView.datasource和tableView.delegate设置为self。我还检查了我是否使用了相同的重用标识符。
错误:正在渲染所有视图,但表视图只是黑色。这是代码:
class MController: UIViewController, UITableViewDelegate, UITableViewDataSource{
let customNavBar = NavBarView()
let statusBarCover = UIView()
var tableView = UITableView()
lazy var kView: UIView = {
return InputView(frame: .init(x: 0, y: 0, width: view.frame.width, height: 100))
}()
override var canBecomeFirstResponder: Bool {
return true
}
override var inputAccessoryView: UIView? {
get {
return kView
}
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.contentInset.top = 120
tableView.verticalScrollIndicatorInsets.top = 120
tableView.keyboardDismissMode = .interactive
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "id")
view.addSubview(customNavBar)
customNavBar.backgroundColor = .yellow
customNavBar.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor, size: .init(width: 0, height: 120))
view.addSubview(statusBarCover)
statusBarCover.backgroundColor = .blue
statusBarCover.anchor(top: view.topAnchor, leading: view.leadingAnchor, bottom: view.safeAreaLayoutGuide.topAnchor, trailing: view.trailingAnchor)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath)
cell.textLabel?.text = "Testing"
return cell
}
}
答案 0 :(得分:0)
根据我在代码中看到的内容,您从未定义过表格视图的大小。
实际上,首先,您甚至没有将表视图添加为子视图。其次,即使您这样做,也存在另一个问题:导航栏和内容具有其默认大小和位置。但是,表视图则没有。我几乎可以肯定,如果启动视图调试器,则可以在视图层次结构中看到表视图,但不能在屏幕上看到,因为默认情况下,它的宽度和高度为零。您需要在layoutSubviews()
方法中手动设置框架,或者在viewDidLoad()
的表视图中设置约束,以使其尺寸大于零。
TL; DR:视图通常不“只是”知道它们应该多大以及应该去哪里。