[英]Swift - Create a UITableViewController with one static cell at the top and the rest be dynamic cells?
[英]How to create static cells with dynamic cell heights with Swift
我看过几个教程,展示了如何设置动态单元格高度,但所有这些教程都只在您通过设置适当的约束和使用UITableViewAutomaticDimension
使用动态单元格时才显示。 但是,我想为静态单元格执行此操作。
我的应用程序中有一个表视图控制器,其中包含多个单元格,这些单元格显示一个带有披露指示符的类别,其中有一个标题,然后是一个描述,描述文本的大小各不相同。 因此,我希望单元格的高度根据描述文本的大小是动态的。
我使用静态单元格而不是动态单元格的原因是因为在类别单元格上方我有一些带有一些设计的单元格,我只能通过使用静态单元格和故事板来获得。
我使用的是iOS9
、 Xcode 7.3
和Swift 2.2
更新
表视图控制器代码
import UIKit
class CategoriesTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 2
}
}
更新 2
根据下面接受的答案,我将下面的两种方法添加到我的表视图控制器中,并删除了 viewDidLoad() 中的 2 行。 这为我做到了! 请记住,我必须确保每个单元格中的所有标签都使用顶部、底部、前导和尾随约束。 此外,每个标签的行数必须设置为 0。
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
用户界面标签:
通过编程设置numberOfLines = 0
或在UILabel
属性检查器中将行设置为零。
界面文本视图:
让我们选择您的文本视图并取消选中启用的滚动,如下图所示。
将以下代码添加到您的视图控制器:
斯威夫特 5:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
斯威夫特 2.2:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
对于 Swift 3
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
您所要做的就是在cell
内设置适当的AutoLayout
,因此高度会根据描述文本的长度增加。 然后你需要在你的viewDidLoad
设置以下内容
tableview.rowHeight = UITableViewAutomaticDimension
tableview.estimatedRowHeight = 44
请注意:您需要将UILabel
的numberOfLines
设置为 0
@Forge,请添加到您的主题
Swift 4 变体:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
@Forge 回答 Swift 5 变体,
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.