UITableView 从其他 Viewcontroller 添加行
UITableView add row from other Viewcontroller
我有两个视图控制器,一个带有 3 个表视图,另一个控制器我有一个 uitextfield 和在文本字段中输入的文本我想将它添加到另一个视图控制器中名为 ScheduleTableView 的表视图之一。
这是我的代码,但在 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)
}
}
vc.ScheduleArray.count - 1
可能是负索引路径
试试这个
if (vc.ScheduleArray.count - 1 >= 0){
vc.ScheduleTableView.insertRows(at: [indexPath], with: .automatic)
}
问题是您试图在 prepare 函数中更新视图控制器。在该函数内部,视图已实例化,但它的 Outlets 尚未连接。
要解决该问题,请按照下列步骤操作:
使用这种方法你应该首先更新你的模型:
@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
// update your model here
self.performSegue(withIdentifier: "secondvc", sender: self)
}
在目标视图控制器中,您应该处理此模型更改并重新加载 table 视图的数据。
override func viewDidLoad() {
self.tableView.reloadData()
}
解决这个问题的办法是改变数组的声明位置(单例就可以),并删除不必要的插入、更新、重新加载等
然后 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)
}
}
我有两个视图控制器,一个带有 3 个表视图,另一个控制器我有一个 uitextfield 和在文本字段中输入的文本我想将它添加到另一个视图控制器中名为 ScheduleTableView 的表视图之一。
这是我的代码,但在 vc.ScheduleTableView.beginUpdates()
@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)
}
}
vc.ScheduleArray.count - 1
可能是负索引路径
试试这个
if (vc.ScheduleArray.count - 1 >= 0){
vc.ScheduleTableView.insertRows(at: [indexPath], with: .automatic)
}
问题是您试图在 prepare 函数中更新视图控制器。在该函数内部,视图已实例化,但它的 Outlets 尚未连接。
要解决该问题,请按照下列步骤操作:
使用这种方法你应该首先更新你的模型:
@IBAction func addButtonTapped(_ sender: YTRoundedButton) {
// update your model here
self.performSegue(withIdentifier: "secondvc", sender: self)
}
在目标视图控制器中,您应该处理此模型更改并重新加载 table 视图的数据。
override func viewDidLoad() {
self.tableView.reloadData()
}
解决这个问题的办法是改变数组的声明位置(单例就可以),并删除不必要的插入、更新、重新加载等
然后 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)
}
}