【发布时间】:2020-05-17 01:22:31
【问题描述】:
目前,我可以将表格视图单元格从一个表格视图移动到另一个表格视图,但在新的表格视图中,我想将此单元格从“标记为已完成”部分移动到“历史记录”部分。现在程序正在运行,但随后在“goals[1].append(goals[0][indexPath.row])”行崩溃,并出现“致命错误:索引超出范围”。我该如何解决这个问题?
更新了进度视图的代码。
let sections: [String] = [“Mark as Complete”, “History”]
var goals: [[String]] = []
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if goals.indices.contains(section) {
return goals[section].count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TodayGoalViewCell_1", for: indexPath) as? GoalTableViewCell
cell?.goalLabel.text = goals[indexPath.section][indexPath.row]
cell?.cellDelegate = self
cell?.index = indexPath
return cell!
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
goals[0].remove(at: indexPath.row)
goals[1].append(goals[0][indexPath.row])
tableView.reloadData()
}
}
在单独的文件中查看常规目标。代码如下:
var goals: [String] = ["goal 1", "goal 2", "goal 3"]
valueToPass = “ ”
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier = “GoalCell_1” {
let viewController = segue.destination as! ViewController
viewController.goals.append([valueToPass])
}
}
extension GoalsViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return goals.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "GoalCell_1", for: indexPath)
cell.textLabel?.text = goals[indexPath.row]
cell.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
cell.textLabel?.numberOfLines = 3
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
valueToPass = goals[indexPath.row]
PerformSegue(withIdentifier: “segue”, sender: self)
if indexPath.section == 0 {
goals.remove(at: indexPath.row)
goals.count != 0 {
showGoalSelected()
tableView.reloadData()
}
}
}
【问题讨论】:
-
如何在两个视图之间移动?
标签: ios swift xcode uitableview