gpt4 book ai didi

swift - 取消隐藏 stackview 的一部分时,stackview 的单元格不会扩展

转载 作者:行者123 更新时间:2023-11-28 15:55:49 26 4
gpt4 key购买 nike

我有一个带有 rowHeight 属性设置为 UITableViewAutomaticDimension 的 TableView 的 View Controller :

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
}

}

接口(interface)在IB中定义如下。它有一个自定义单元格。该单元格包含一个堆栈 View 。在该堆栈 View 的顶部,有一个按钮,在底部,有一个文本字段。文本字段最初设置为隐藏。

UITableView with prototype cell containing stackview

点击按钮将取消隐藏文本字段。但是,单元格不会展开:

Screenshot of iOS Simulator

预期的是单元格展开以显示整个堆栈 View 。为什么这行不通?

(我已经通过重新加载单元使其工作,但这是一个丑陋的解决方法,需要在 View Controller 中编写代码。)

Github 上的项目: https://github.com/bvankuik/TestResizingCell

最佳答案

我已经“修复”了如下问题。在 View Controller 中,我维护一行的文本字段是隐藏还是可见。我删除了按钮,只使用了 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

关于swift - 取消隐藏 stackview 的一部分时,stackview 的单元格不会扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41765577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com