UIView 内部的 UITableView
UITableView inside UIView
我正在尝试在 xib 中创建一个动态的 UITableView,所以我认为可能可行的方法是将 table 视图放在一个空白的 UIView 中。然后我将 subclass 这个 UIView 并使其遵守协议 UITableViewDelegate
和 UITableViewDataSource
。有人可以指导我吗,因为我已经尝试了很多东西,但是 none 成功了。非常感谢!
编辑:
我会告诉你我之前试过的(抱歉,没有原始代码):
class TableControllerView: UIView, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView()
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
tableView.frame = self.frame
tableView.delegate = self
tableView.dataSource = self
}
func tableView(...cellForRowAt...) {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.titleLabel?.text = "test title"
return cell
}
}
但在这种情况下,table 视图最终变得太大并且与视图不对齐,即使我将它的框架设置为与视图相同
编辑 2:
我的代码现在看起来像这样:
class PharmacyTableView: UIView, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var pharmacyTableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//PROVISOIRE depends on user [medications].count
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = "text label test"
cell.detailTextLabel?.text = "detail label test"
return cell
}}
table 视图已初始化,但仅在高度锚点受限时显示,这是我不想要的,因为它可能会根据用户数据增长或缩小。我想我会在解决这个问题后完成?
P.S。 : 另外,非常感谢那些花时间帮助我的人 :)
编辑 3:
因此,我将 table 视图的 class 更改为:
class IntrinsicResizingTableView: UITableView {
override var contentSize:CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height)
}
}
现在一切正常!终于!
你的问题不清楚。根据您的最终陈述,您应该使用以下代码来保持 table 视图及其父视图对齐。
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
如果这不是您要找的答案,请说明您的问题。如果您解释一下您这样做的目的,它也会有所帮助吗?为什么需要这个超级视图?
我正在尝试在 xib 中创建一个动态的 UITableView,所以我认为可能可行的方法是将 table 视图放在一个空白的 UIView 中。然后我将 subclass 这个 UIView 并使其遵守协议 UITableViewDelegate
和 UITableViewDataSource
。有人可以指导我吗,因为我已经尝试了很多东西,但是 none 成功了。非常感谢!
编辑:
我会告诉你我之前试过的(抱歉,没有原始代码):
class TableControllerView: UIView, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView()
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
tableView.frame = self.frame
tableView.delegate = self
tableView.dataSource = self
}
func tableView(...cellForRowAt...) {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.titleLabel?.text = "test title"
return cell
}
}
但在这种情况下,table 视图最终变得太大并且与视图不对齐,即使我将它的框架设置为与视图相同
编辑 2:
我的代码现在看起来像这样:
class PharmacyTableView: UIView, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var pharmacyTableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//PROVISOIRE depends on user [medications].count
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = "text label test"
cell.detailTextLabel?.text = "detail label test"
return cell
}}
table 视图已初始化,但仅在高度锚点受限时显示,这是我不想要的,因为它可能会根据用户数据增长或缩小。我想我会在解决这个问题后完成? P.S。 : 另外,非常感谢那些花时间帮助我的人 :)
编辑 3:
因此,我将 table 视图的 class 更改为:
class IntrinsicResizingTableView: UITableView {
override var contentSize:CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height)
}
}
现在一切正常!终于!
你的问题不清楚。根据您的最终陈述,您应该使用以下代码来保持 table 视图及其父视图对齐。
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
如果这不是您要找的答案,请说明您的问题。如果您解释一下您这样做的目的,它也会有所帮助吗?为什么需要这个超级视图?