【发布时间】:2020-05-20 14:32:42
【问题描述】:
我正在创建一个表格视图,一旦目标被选中,它就会移动到另一个表格视图。但是,当我从第一个表视图中选择一个单元格时,我收到一个致命错误:第二个视图控制器中的“返回部分 [部分]”行的索引超出范围。该程序在不使用标题的情况下正常运行。我将如何解决这个问题,以便它运行并显示每个部分的标题视图?
这是我的第一个 tableview 的代码。
import UIKit
class GoalsViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var goals: [String] = ["goal 1", "goal 2", "goal 3"]
var valueToPass = ""
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
let viewController = segue.destination as! GoalsTwoViewController
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)
if goals.count != 0 {
showGoalSelected()
tableView.reloadData()
}
}
}
这是我的第二个表格视图的代码。
import UIKit
class GoalsTwoViewController: UIViewController {
@IBOutlet weak var goalTableView: UITableView!
let sections: [String] = ["Mark as Complete:", "History:"]
var goals: [[String]] = [[], []]
let theEmptyModel: [String] = ["No data in this section."]
extension GoalsViewTwoController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return goals[section].count
}
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 goals.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
progressBarAnimation()
if goals[0] != theEmptyModel {
goals[1].append(goals[0][indexPath.row])
if goals[1].first!.contains("No data in this section.") {
goals[1].removeFirst()
}
goals[0].remove(at: indexPath.row)
if goals[0].count == 0 {
goals[0].append(contentsOf: theEmptyModel)
}
}
tableView.reloadData()
}
【问题讨论】:
-
你在哪一行遇到了崩溃?
-
在GoalsTwoViewController的扩展中返回sections[section]
-
numberOfSections(in tableView:)returnsgoals.count。tableView(_ tableView: titleForHeaderInSection:)返回sections[section]。谁告诉你sections.count == goals.countviewController.goals.append([valueToPass])到goals是做什么的?
标签: ios swift xcode uitableview header