如何解决将数据从表格单元格发送到视图控制器时委托属性为零
我的委托模式有问题,我试图将数据从表视图单元格发送到另一个视图控制器,但是当我在第二个控制器(接收器)中设置委托属性时,它为零,谁能向我解释一下,我是新手
协议和发送者类:
import UIKit
protocol SendDataDelegate {
func updateProperties(_ data: CellData)
}
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var arr: [CellData] = [
CellData(img: UIImage(named: "1")!,str1: "welcome1",str2: "hello1"),CellData(img: UIImage(named: "2")!,str1: "welcome2",str2: "hello2"),CellData(img: UIImage(named: "3")!,str1: "welcome3",str2: "hello3"),CellData(img: UIImage(named: "4")!,str1: "welcome4",str2: "hello4"),CellData(img: UIImage(named: "5")!,str1: "welcome5",str2: "hello5"),CellData(img: UIImage(named: "6")!,str1: "welcome6",str2: "hello6"),CellData(img: UIImage(named: "7")!,str1: "welcome7",str2: "hello7"),CellData(img: UIImage(named: "8")!,str1: "welcome8",str2: "hello8"),CellData(img: UIImage(named: "9")!,str1: "welcome9",str2: "hello9"),CellData(img: UIImage(named: "10")!,str1: "welcome10",str2: "hello10")
]
var delegate: SendDataDelegate?
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
fileprivate func setupTableView() {
tableView.dataSource = self
tableView.delegate = self
tableView.register(
UINib(nibName: Constants.cellNibName,bundle: nil),forCellReuseIdentifier: Constants.cellIdentifier
)
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return arr.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(
withIdentifier: Constants.cellIdentifier,for: indexPath
) as! TableCell
cell.picture.image = arr[indexPath.row].img
cell.label1.text = arr[indexPath.row].str1
cell.label2.text = arr[indexPath.row].str2
return cell
}
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat(300.0)
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath,animated: true)
let storyboard = UIStoryboard(name: Constants.storyboardName,bundle: nil)
let nextController = storyboard.instantiateViewController(
withIdentifier: Constants.destinationController
) as! SecondController
var data = arr[indexPath.row]
delegate?.updateProperties(data)
present(nextController,animated: true,completion: nil)
}
}
这里是接收器类:
import UIKit
class SecondController: UIViewController {
@IBOutlet weak var picture: UIImageView! {
didSet {
picture.image = img
}
}
@IBOutlet weak var label11: UILabel! {
didSet {
label11.text = s1
}
}
@IBOutlet weak var label22: UILabel! {
didSet {
label22.text = s2
}
}
var s1 = ""
var s2 = ""
var img = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemRed
let controller = UIStoryboard(name: Constants.storyboardName,bundle: nil).instantiateViewController(
withIdentifier: Constants.firstController
) as! ViewController
controller.delegate = self
print(s1)
}
}
extension SecondController: SendDataDelegate {
func updateProperties(_ data: CellData) {
self.s1 = data.str1
self.s2 = data.str2
self.img = data.img
}
}
解决方法
这里不需要委托,只需要传递模型项
stage('Static Analysis') {
steps {
script{
bat'call "C:\\Program Files\\Cppcheck\\cppcheck.exe" D:\\Source\\Game --xml --xml-version=2 . 2> cppcheck.xml'
}
}
}
里面nextController.sendedItem = arr[indexPath.row]
SecondController
里面的var sendedItem:CellData?
设置值
编辑:
viewDidLoad
,
好的,我想通了,在这个函数的发送者类中:
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath,animated: true)
let storyboard = UIStoryboard(name: Constants.storyboardName,bundle: nil)
let nextController = storyboard.instantiateViewController(
withIdentifier: Constants.destinationController
) as! SecondController
var data = arr[indexPath.row]
delegate?.updateProperties(data)
present(nextController,animated: true,completion: nil)
}
}
我在将数据传递给 updateProperties() 函数之前添加了这一行 delegate = nextController
并且有效
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。