问题显示从一个视图控制器到另一个视图控制器的表视图单元格

时间:2020-05-18 18:20:17

标签: ios swift xcode uitableview

现在,我正在尝试将信息从目标单元格移到新的表格视图单元格中,并且难以显示该单元格。

这是我目标单元的代码。

import UIKit

    class GoalsViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var Goals: [String] = ["goal 1", "goal 2", "goal 3"]
    let theEmptyModel: [String] = ["No data in this section."]
    var valueToPass = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }

    func showGoalSelected() {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
            let popUp = GoalSelectedPopUp()
            self.view.addSubview(popUp)
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "GoalConversationsCell_1") {
            let viewController = segue.destination as! ActiveGoalsViewController
            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) {
        if indexPath.section == 0 {
            valueToPass = Goals[indexPath.row]
            performSegue(withIdentifier: "activeGoalsSegue", sender: self)
        Goals.remove(at: indexPath.row)
        if Goals.count != 0 {
            showGoalSelected()
        } else {
            Goals.append(contentsOf: theEmptyModel)
        }
        tableView.reloadData()
    }
} 

这是目标单元情节提要板,其中推推功能将其连接到另一个表格视图。

goal table view with connection of a push segue

另一个表格视图如下所示。 other table view in which I am trying to send the cell

这是此新表格视图的代码。

import UIKit

class ActiveGoalsViewController: 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 ActiveGoalsViewController: 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 {


            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()
                }
            }
      }

一旦选择了目标,它将把我发送到新的情节提要,但是此新视图不会显示刚刚添加的目标。有人可以帮我弄清楚为什么这行不通吗?谢谢。

1 个答案:

答案 0 :(得分:1)

我认为在第二个视图控制器中,您需要访问小写字母g的“目标”变量,而不是大写字母G的“目标”变量。