【问题标题】:Trouble with titleForHeaderInSectiontitleForHeaderInSection 的问题
【发布时间】: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:)returns goals.counttableView(_ tableView: titleForHeaderInSection:) 返回sections[section]。谁告诉你sections.count == goals.countviewController.goals.append([valueToPass])goals是做什么的?

标签: ios swift xcode uitableview header


【解决方案1】:
var goals: [[String]] = [[], []] // wrong
var goals: [[String]] = [] // It would be like this

func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if goals.indices.contains(section) {
        return goals[section].count
    }
    return 0
}


运行并再次检查

【讨论】:

  • 谢谢,但现在只显示一个剖面视图而不是两个。
  • 请检查上面我的代码,我刚刚更新了
【解决方案2】:

修改你numberOfSection如下:

func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}

【讨论】:

  • 当我运行这个时,我不再看到表格视图单元格被添加到第二个表格视图中。
  • @k-12345 这超出了这个问题的范围。这个答案是为了解决崩溃。如果您有更多与此问题无关的疑问,请发布另一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多