gpt4 book ai didi

ios - UITableView 从其他 Viewcontroller 添加行

转载 作者:行者123 更新时间:2023-11-28 05:58:40 26 4
gpt4 key购买 nike

我有两个 View Controller ,一个带有 3 个 TableView ,另一个 Controller 我有一个 uitextfield 和在文本字段中输入的文本我想将它添加到另一个 View Controller 中名为 ScheduleTableView 的 TableView 之一。这是我的代码,但在 vc.ScheduleTableView.beginUpdates()

解包可选值时出现错误 unwrappedly found nil
@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
self.performSegue(withIdentifier: "secondvc", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "secondvc" {
print(TitleTextField.text!)

let vc = segue.destination as! GrowthMainViewController

vc.ScheduleArray.append(TitleTextField.text!)
let indexPath = IndexPath(row: vc.ScheduleArray.count - 1, section: 0)
vc.ScheduleTableView.beginUpdates()
vc.ScheduleTableView.insertRows(at: [indexPath], with: .automatic)
vc.ScheduleTableView.reloadData()
vc.ScheduleTableView.endUpdates()



TitleTextField.text = ""
view.endEditing(true)

}
}

最佳答案

对此的解决方案是更改声明数组的位置(单例对此很好),并删除不必要的插入、更新、重新加载等。

然后 numRowsInSection 方法调用 scheduleArray.count 来显示所有相应的数据。

之前:

@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
self.performSegue(withIdentifier: "secondvc", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "secondvc" {
print(TitleTextField.text!)

let vc = segue.destination as! GrowthMainViewController

vc.ScheduleArray.append(TitleTextField.text!)
let indexPath = IndexPath(row: vc.ScheduleArray.count - 1, section: 0)
vc.ScheduleTableView.beginUpdates()
vc.ScheduleTableView.insertRows(at: [indexPath], with: .automatic)
vc.ScheduleTableView.reloadData()
vc.ScheduleTableView.endUpdates()



TitleTextField.text = ""
view.endEditing(true)

}
}

之后:

@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
self.performSegue(withIdentifier: "secondvc", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "secondvc" {

guard let text = TitleTextField.text else { return }
scheduleArray.append(text)
TitleTextField.text = ""
view.endEditing(true)

}
}

关于ios - UITableView 从其他 Viewcontroller 添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50588090/

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