【问题标题】:Issues displaying a table view cell from one view controller to another从一个视图控制器到另一个视图控制器显示表格视图单元格的问题
【发布时间】:2020-05-18 18:20:17
【问题描述】:

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

这是我的目标单元格的代码。

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

这里是目标单元的故事板,通过 push segue 将其连接到另一个表视图。

另一个表格视图如下所示。

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

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

选择目标后,它会将我转到新的故事板,但这个新视图不会显示刚刚添加的目标。有人可以帮我弄清楚为什么这不起作用吗?谢谢。

【问题讨论】:

  • 我没有看到ActiveGoalsViewController中的tableView数据源或委托集

标签: ios swift xcode uitableview


【解决方案1】:

我认为在第二个视图控制器中,您需要使用小写 g 访问“goals”变量,而不是使用大写 G 访问“Goals”变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    相关资源
    最近更新 更多