目前,我可以将表视图单元格从一个表视图移动到另一个表视图,但是在新表视图中,我想将此单元格从"标记为完成"部分移动到"历史记录"部分。现在程序运行,但随后在"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()
}
}
}
因此,通过使用 segue 在 2 行之间移动,您可以使用 didSelectRowAtIndexPath(( 函数来选择目标并通过 segue 将其传递给进度视图。 为此,您需要在进度视图中排列以接受您拥有的目标。
var goals: [String] = []
您还需要设置一个 segue,最简单的方法是按住 ctrl 单击并从目标视图的故事板顶部的 viewController 图标中拖动,然后将其放在进度目标的视图上。您将获得一个下拉列表以选择 segue 类型,选择 1。然后使用右侧菜单将 segue 命名为令人难忘的东西。
你还需要一个地方来容纳要通过的目标。 将此添加到您的目标视图中。
var valueToPass = ""
现在实现像这样的 did 选择行函数。
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
print("You selected cell #(indexPath.row)!")
//selects the goal from the goals array
valueToPass = goals[indexPath.row]
//trigger the segue to move views
performSegue(withIdentifier: <yourseguename>, sender: self)
}
现在,在 segue 中将值传递给进度目标视图。
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "yourseguename"){
//this is the view controller of the where progress goals table is
let viewController = segue.destination as! ViewController
//add the goal you selected to the array in you progress goals view.
viewController.goals.append(valueToPass)
}
}
希望有帮助