Swift- 使用委托在 tableView 上显示

Swift- Using Delegation to display on tableView

在尝试学习使用 Swift 中的委托。问题是我使用委托将字符串值(从 pickCourse()ViewController())附加到 sampleArr;但是,它似乎确实附加了但未显示在 ViewController() 的表格视图中。我尝试使用 viewWillAppear 但我没有运气。有谁知道在 pickCourses() 中选择 someString 值后如何让 sampleArr 出现在 ViewController() 的表格视图中,该值会自动弹出到 ViewController()

import UIKit

class ViewController: UITableViewController, testingDelegate {

    let cellId = "Something"

    var sampleArr : [String] = []

    func sendDataback(data: String) {
        sampleArr.append(data)
        tableView.reloadData()
    }
    override func viewWillAppear(_ animated: Bool) {
        tableView.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)

        navigationItem.title = "Testing Delegate??"
        navigationController?.navigationBar.prefersLargeTitles = true

        self.view.backgroundColor = UIColor.white

        let button1 = UIBarButtonItem(title: "Add A String", style: .plain, target: self, action: #selector(addTapped))
        self.navigationItem.rightBarButtonItem = button1

    }

    @objc func addTapped(sender: UIBarButtonItem!) {

        let destination = PickCourses()
        destination.delegate = self
        navigationController?.pushViewController(destination, animated: true)
    }

}
import UIKit

protocol testingDelegate {
    func sendDataback(data: String)
}

class PickCourses: UITableViewController {

    var delegate: testingDelegate?

    let cellId = "leariningtopassdata"

    let someString = [
        "One One One One",
        "Two Two Two Two",
        "Three Three Three Three",
        "Four Four Four",
        "Five Five Five"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "Pick Some String"
        navigationController?.navigationBar.prefersLargeTitles = true

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        tableView.delegate = self
        tableView.dataSource = self

        self.tableView.tableFooterView = UIView() //removes extra line in UITableView

        tableView.isScrollEnabled = false //disables scrolling
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return someString.count
        }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

        let stringlabel = self.someString[indexPath.row]
        cell.textLabel?.text = stringlabel

        return cell
    }

    let vc = ViewController()

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

        tableView.deselectRow(at: indexPath, animated: true)
        let data = someString[indexPath.row]
        delegate?.sendDataback(data: data)
        self.navigationController?.popViewController(animated: true)
    }


}

你应该阅读 UITableViewController:

You must also override the data source and delegate methods required to fill your table with data.

我找不到您在 ViewController 中覆盖 the data source methods 的位置。您仅在 PickCourses 中执行此操作。

您应该在您的 calss 声明中插入以下内容:

class YourClassName: UIViewController, UITableViewDataSource, UITableViewDelegate {

这将提示您添加以下委托方法:

cellForRowAt, numbersOfSection, numbersOfRowInSection, 

实现所有这些之后,你可以在 cellForRow 方法中实现类似这样的东西:

let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
    cell.textLabel?.text = sampleArr![indexPath.row]
    return cell

希望对您有所帮助。