ios - Swift-使用委托(delegate)在 tableView 上显示

标签 ios arrays swift uitableview delegates

在尝试学习在 Swift 中使用委托(delegate)。问题是我使用委托(delegate)将字符串值(从 pickCourse()ViewController() )附加到 sampleArr ;然而,它似乎是附加的,但没有出现在 ViewController() 上。的 TableView 。我尝试使用 viewWillAppear但我没有运气。有谁知道我如何获得 sampleArr出现在 ViewController()选择 someString 后的 tableview pickCourses() 中的值自动弹出到 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.



我找不到您在哪里覆盖 the data source methods在 View Controller 中。您仅在 PickCourses 中执行此操作。

关于ios - Swift-使用委托(delegate)在 tableView 上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60224105/

相关文章:

ios - 如何按应用程序语言本地化 iOS 通知

ios - 不同时间重复通知

swift - 在 Swift 中从 Array of Dictionary of Array 中过滤项目

c# - 对与另一个数组相关的数组进行排序

arrays - 将 IndexPath.row 转换为二维数组

ios - 我可以在 IOS 8.1 中的 xib 文件中使用大小类吗?

ios - 如何在 iOS 中播放 .pls 文件?

php - Kotlin(Android) MD5 检查与 PHP 签名

ios - 关于 Property not found Xcode 5 错误

arrays - Perl:从数组创建哈希