简体   繁体   中英

Swift- Using Delegation to display on tableView

In trying to learn to use the delegate in Swift. The problem is I used the delegate to append a string value (from pickCourse() to ViewController() ) to sampleArr ; however, it does seem to append but does not show up on the ViewController() 's tableview. I tried to use viewWillAppear but I have no luck. Does anyone know how I can get the sampleArr to appear on ViewController() 's tableview after I selected a someString value in pickCourses() which automatically popbacks to 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)
    }


}

You should read UITableViewController :

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

I can't find where you override the data source methods in ViewController. You did this only in PickCourses.

You should insert following to your calss declaration:

class YourClassName: UIViewController, UITableViewDataSource, UITableViewDelegate {

This will prompt you to add delegate methods, which are following:

cellForRowAt, numbersOfSection, numbersOfRowInSection, 

After implementing all them, you can implement something like this into the cellForRow method:

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

I hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM