取消隐藏 stackview 的一部分时,stackview 的单元格不会扩展
Cell with stackview not expanding when unhiding part of stackview
我有一个 viewcontroller 的 tableview,其 rowHeight 属性 设置为 UITableViewAutomaticDimension:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var items: [String] = []
@IBOutlet weak var tableView: UITableView!
// MARK: - UITableViewDataSource, UITableViewDelegate
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell.expandButton.setTitle(self.items[indexPath.row], for: .normal)
return cell
}
// MARK: - View cycle
override func viewDidLoad() {
super.viewDidLoad()
items = [
"Allen","Upton","Hu","Yuli","Tiger","Flynn","Lev","Kyle","Sylvester","Mohammad",
"Harlan","Julian","Sebastian","Porter","Preston","Palmer","Jakeem","Micah","Stephen","Tucker"
]
self.tableView.estimatedRowHeight = 150
self.tableView.rowHeight = UITableViewAutomaticDimension
}
}
接口在IB中定义如下。它有一个自定义单元格。该单元格包含一个堆栈视图。在该堆栈视图的顶部,有一个按钮,在底部,有一个文本字段。文本字段最初设置为隐藏。
点击按钮将取消隐藏文本字段。但是,单元格不会展开:
预期是单元格展开以显示整个堆栈视图。为什么这不起作用?
(我已经通过重新加载单元使其工作,但这是一个丑陋的解决方法,需要 viewcontroller 中的代码。)
Github 上的项目:
https://github.com/bvankuik/TestResizingCell
不幸的是,当您更改其中的约束时,自调整单元格高度不会自动更改。
当您生成 tableView 时,委托的方法是调用:numberOfSections
、numberOfRowsInSection
、cellForRowAt
等...
tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
也是如此。这意味着如果您在更改单元格中的约束后不重新加载 tableView,则不会调用这些委托的方法,并且不会通过自动布局再次计算行高。
最好的建议是在显示文本字段后立即调用 cell.layoutIfNeeded()
,而不是重新加载 tableView。
我"fixed"问题如下。在 viewcontroller 中,我维护一行的文本字段是隐藏还是可见。我删除了按钮,只使用 didSelectRowAt
。重新加载单元格:
struct NameCellData {
var name: String
var isTextFieldHidden: Bool
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var items: [NameCellData] = []
@IBOutlet weak var tableView: UITableView!
// MARK: - UITableViewDataSource, UITableViewDelegate
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell.nameLabel.text = self.items[indexPath.row].name
cell.nameCorrectionTextField.isHidden = self.items[indexPath.row].isTextFieldHidden
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.items[indexPath.row].isTextFieldHidden = !self.items[indexPath.row].isTextFieldHidden
tableView.reloadRows(at: [indexPath], with: .automatic)
}
// MARK: - View cycle
override func viewDidLoad() {
super.viewDidLoad()
let names = [
"Allen","Upton","Hu","Yuli","Tiger","Flynn","Lev","Kyle","Sylvester","Mohammad",
"Harlan","Julian","Sebastian","Porter","Preston","Palmer","Jakeem","Micah","Stephen","Tucker"
]
for name in names {
let nameCellData = NameCellData(name: name, isTextFieldHidden: true)
items.append(nameCellData)
}
self.tableView.estimatedRowHeight = 150
self.tableView.rowHeight = UITableViewAutomaticDimension
}
}
有关完整代码,请参阅 Github 上原始测试项目的分支:
https://github.com/bvankuik/TestResizingCell/tree/withReload
我有一个 viewcontroller 的 tableview,其 rowHeight 属性 设置为 UITableViewAutomaticDimension:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var items: [String] = []
@IBOutlet weak var tableView: UITableView!
// MARK: - UITableViewDataSource, UITableViewDelegate
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell.expandButton.setTitle(self.items[indexPath.row], for: .normal)
return cell
}
// MARK: - View cycle
override func viewDidLoad() {
super.viewDidLoad()
items = [
"Allen","Upton","Hu","Yuli","Tiger","Flynn","Lev","Kyle","Sylvester","Mohammad",
"Harlan","Julian","Sebastian","Porter","Preston","Palmer","Jakeem","Micah","Stephen","Tucker"
]
self.tableView.estimatedRowHeight = 150
self.tableView.rowHeight = UITableViewAutomaticDimension
}
}
接口在IB中定义如下。它有一个自定义单元格。该单元格包含一个堆栈视图。在该堆栈视图的顶部,有一个按钮,在底部,有一个文本字段。文本字段最初设置为隐藏。
点击按钮将取消隐藏文本字段。但是,单元格不会展开:
预期是单元格展开以显示整个堆栈视图。为什么这不起作用?
(我已经通过重新加载单元使其工作,但这是一个丑陋的解决方法,需要 viewcontroller 中的代码。)
Github 上的项目: https://github.com/bvankuik/TestResizingCell
不幸的是,当您更改其中的约束时,自调整单元格高度不会自动更改。
当您生成 tableView 时,委托的方法是调用:numberOfSections
、numberOfRowsInSection
、cellForRowAt
等...
tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
也是如此。这意味着如果您在更改单元格中的约束后不重新加载 tableView,则不会调用这些委托的方法,并且不会通过自动布局再次计算行高。
最好的建议是在显示文本字段后立即调用 cell.layoutIfNeeded()
,而不是重新加载 tableView。
我"fixed"问题如下。在 viewcontroller 中,我维护一行的文本字段是隐藏还是可见。我删除了按钮,只使用 didSelectRowAt
。重新加载单元格:
struct NameCellData {
var name: String
var isTextFieldHidden: Bool
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var items: [NameCellData] = []
@IBOutlet weak var tableView: UITableView!
// MARK: - UITableViewDataSource, UITableViewDelegate
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell.nameLabel.text = self.items[indexPath.row].name
cell.nameCorrectionTextField.isHidden = self.items[indexPath.row].isTextFieldHidden
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.items[indexPath.row].isTextFieldHidden = !self.items[indexPath.row].isTextFieldHidden
tableView.reloadRows(at: [indexPath], with: .automatic)
}
// MARK: - View cycle
override func viewDidLoad() {
super.viewDidLoad()
let names = [
"Allen","Upton","Hu","Yuli","Tiger","Flynn","Lev","Kyle","Sylvester","Mohammad",
"Harlan","Julian","Sebastian","Porter","Preston","Palmer","Jakeem","Micah","Stephen","Tucker"
]
for name in names {
let nameCellData = NameCellData(name: name, isTextFieldHidden: true)
items.append(nameCellData)
}
self.tableView.estimatedRowHeight = 150
self.tableView.rowHeight = UITableViewAutomaticDimension
}
}
有关完整代码,请参阅 Github 上原始测试项目的分支: https://github.com/bvankuik/TestResizingCell/tree/withReload