自调整 UITableView 单元格的大小不正确
Self sizing UITableView cell not sizing correctly
我有一个 UITableView
静态单元格。描述标签应该显示多行文本,但只显示一行。
我选择了描述标签并将行数设置为 0。我也将其设置为自动换行。
我已将自动尺寸设置如下:
class BeerTableViewController: UITableViewController {
@IBOutlet weak var beerName: UILabel!
@IBOutlet weak var brewer: UILabel!
@IBOutlet weak var beerDescription: UILabel!
var scannedBeer: Beer!
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
beerName.text = scannedBeer.name
brewer.text = scannedBeer.brewer
beerDescription.text = scannedBeer.description
}
override func viewDidAppear(_ animated: Bool) {
tableView.reloadData()
}
}
我已将标签固定到 ContentView 的顶部和底部,如下所示:
尝试使用 heightForRowAt indexPath:
和 estimatedHeightForRowAt indexPath:
of UITableViewDelegate
, return UITableViewAutomaticDimension
.
只需将 table 视图 delegate
设置为 self
并使用以下内容:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
这是因为您在 ViewDidLoad 上的 beereDescription 标签为 nil。您需要在 cellForRowAtIndexpath 而不是 ViewDidLoad 上赋值。
并且您无法在 UIViewcontroller 中连接单元格子视图的 IBOutlet class。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "cellIdentifier"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier)
beerDescription: UILabel? = (newsCell?.viewWithTag(101) as? UILabel) // assign tag to label in cell
beerDescription.text = scannedBeer.description
return cell
}
我有一个 UITableView
静态单元格。描述标签应该显示多行文本,但只显示一行。
我选择了描述标签并将行数设置为 0。我也将其设置为自动换行。
我已将自动尺寸设置如下:
class BeerTableViewController: UITableViewController {
@IBOutlet weak var beerName: UILabel!
@IBOutlet weak var brewer: UILabel!
@IBOutlet weak var beerDescription: UILabel!
var scannedBeer: Beer!
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
beerName.text = scannedBeer.name
brewer.text = scannedBeer.brewer
beerDescription.text = scannedBeer.description
}
override func viewDidAppear(_ animated: Bool) {
tableView.reloadData()
}
}
我已将标签固定到 ContentView 的顶部和底部,如下所示:
尝试使用 heightForRowAt indexPath:
和 estimatedHeightForRowAt indexPath:
of UITableViewDelegate
, return UITableViewAutomaticDimension
.
只需将 table 视图 delegate
设置为 self
并使用以下内容:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
这是因为您在 ViewDidLoad 上的 beereDescription 标签为 nil。您需要在 cellForRowAtIndexpath 而不是 ViewDidLoad 上赋值。
并且您无法在 UIViewcontroller 中连接单元格子视图的 IBOutlet class。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "cellIdentifier"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier)
beerDescription: UILabel? = (newsCell?.viewWithTag(101) as? UILabel) // assign tag to label in cell
beerDescription.text = scannedBeer.description
return cell
}