如何解决如何基于用户输入在表视图中快速创建动态节?
我正在尝试根据用户在文本字段中的输入创建多个部分。 看看我的快照,使它变得更容易。
默认情况下,我创建了标签1,标签2,标签3,标签4的1个部分。
但是如果将部分文本字段的数量输入3,则我希望将用标签填充的部分增加到3。
我怎么做?
我的问题是,当我键入它时,它不会合计另一个部分。
我也将tableView.reloadData()
放在了一些可能的位置,但是没有用。
这是我的代码,任何建议将不胜感激。
先谢谢您
var numberOfSection: String = "0"
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
if numberOfSection == "0" {
return 2
}
else if numberOfSection == "1" {
return 2
}
else {
print(numberOfSection)
return Int(numberOfSection) ?? 2
}
}
override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 1
case 1:
return WSSpec.count
default:
return 0
}
}
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let cells = tableView.dequeueReusableCell(withIdentifier: "serverNumbCell",for: indexPath) as! CellOutlet
let txtFieldServerNumber = cells.txtFieldServerNumb
cells.lblSectionNumb.text = "Number of section:"
txtFieldServerNumber?.addTarget(self,action: #selector(sectionTxtFieldDidChange),for: .editingChanged)
return cells
case 1:
let cells = tableView.dequeueReusableCell(withIdentifier: "serverSpecCell",for: indexPath) as! CellOutlet
let specWS = testSpec[indexPath.row]
cells.labels.text = specWS.spec
return cells
default:
return CellOutlet()
}
}
@objc func sectionTxtFieldDidChange(_ sender: UITextField) {
numberOfSection = sender.text ?? "2"
}
解决方法
尝试将didSet添加到变量,然后从那里重新加载数据。像这样:
var numberOfSection: String = "0" {
didSet {
tableView.reloadData()
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。