Jac*_*ace 8 ios swift xcode7.3
我已经看了几个教程,展示了如何设置动态单元格高度,但是如果你通过设置适当的约束和使用动态单元格,它们都只显示它UITableViewAutomaticDimension.但是,我想对静态细胞这样做.
我的应用程序中有一个表视图控制器,其中包含几个单元格,这些单元格显示带有公开指示符的类别,其中有标题,然后是描述,描述文本的大小各不相同.因此,我希望单元格的高度根据描述文本的大小是动态的.
我使用静态单元而不是动态单元的原因是因为在类别单元格之上我有一些具有一些设计的单元格,我只能通过使用静态单元格和故事板来获得.
我正在使用iOS9,Xcode 7.3和Swift 2.2
UPDATE
表视图控制器代码
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
}
}
Run Code Online (Sandbox Code Playgroud)
更新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
}
Run Code Online (Sandbox Code Playgroud)
Raj*_*n S 11
您正在使用每个单元格的文本视图.好的,让我们选择您的文本视图并取消选中启用滚动,如下图所示.
将以下代码添加到视图控制器:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Run Code Online (Sandbox Code Playgroud)
对于Swift 3
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6055 次 |
| 最近记录: |