gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:03:49 25 4
gpt4 key购买 nike

在尝试学习在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com