如何解决如何将步进值传递给ViewController?
我有一个具有2个标签的自定义单元格,myLabel和numLabel,以及一个步进器。我的Swift档案和XIB档案中都有我的自订储存格。我想在步进器上单击+或-按钮时,numLabel随步进器的值而变化。我不知道如何将步进值传递给我的tableView所在的viewController。以后想将步进值保存到CoreDate中,该怎么办?我只是一个初学者。谢谢您的帮助。
MyCell.swift
import UIKit
class MyCell: UITableViewCell {
static let identifier = "MyCell"
static func nib() -> UINib {
return UINib(nibName: "MyCell",bundle: nil)
}
public func configure(with name: String,number: String) {
myLabel.text = name
numLabel.text = number
}
@IBOutlet var myLabel: UILabel!
@IBOutlet var numLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool,animated: Bool) {
super.setSelected(selected,animated: animated)
// Configure the view for the selected state
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
table.register(MyCell.nib(),forCellReuseIdentifier: MyCell.identifier)
table.delegate = self
table.dataSource = self
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MyCell.identifier,for: indexPath) as! MyCell
cell.configure(with: "Item 1",number: "1")
return cell
}
}
解决方法
您可以通过“回调”关闭轻松完成此操作:
class MyCell: UITableViewCell {
static let identifier: String = "MyCell"
@IBOutlet var myStepper: UIStepper!
@IBOutlet var numLabel: UILabel!
@IBOutlet var myLabel: UILabel!
// "callback" closure - set my controller in cellForRowAt
var callback: ((Int) -> ())?
public func configure(with name: String,number: String) {
myLabel.text = name
numLabel.text = number
}
@IBAction func stepperChanged(_ sender: UIStepper) {
let val = Int(sender.value)
numLabel.text = "\(val)"
// send value back to controller via closure
callback?(val)
}
static func nib() -> UINib {
return UINib(nibName: "MyCell",bundle: nil)
}
}
然后,在cellForRowAt
中:
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MyCell.identifier,for: indexPath) as! MyCell
cell.configure(with: "Item 1",number: "1")
// set the "callback' closure
cell.callback = { (val) in
print("Stepper in cell at \(indexPath) changed to: \(val)")
// do what you want when the stepper value was changed
// such as updating your data array
}
return cell
}
,
将委托用于通用方法。这样可以灵活地实现单元格与表格视图的交互方式,并实现Swift所期望的类型检查。
通常,对于UITableView,您将具有驱动单元内容的数据数组。就您而言,我们假设它是MyStruct(在视图控制器内部):
struct MyStruct {
let name: String
var value: Int
}
var myStructs: [ MyStruct ] = [
MyStruct( name: "Name 1",value: 1 ),MyStruct( name: "Name 2",value: 2 ),MyStruct( name: "Name 3",value: 3 ) ]
创建MyCellDelegate
,并在其中放置将更改从单元格传递到视图控制器所需的任何方法。例如:
protocol MyCellDelegate: class {
func didSet( value: Int,for myStructIndex: Int )
}
class MyCell: UITableViewCell {
weak var delegate: MyCellDelegate!
var myStructIndex: Int!
...
}
对于表视图,在使单元出队时分配委托,并实现协议。
class ViewController: MyCellDelegate,UITableViewDelegate,UITableViewDataSource {
func tableView( _ tableView: UITableView,cellForRowAt indexPath: IndexPath ) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MyCell.identifier,for: indexPath) as! MyCell
let myStruct = myStructs[indexPath.row] // You may want to ensure that you are in bounds
cell.delegate = self
cell.myStructIndex = indexPath.row
cell.configure( with: myStruct.name,number: myStruct.value )
return cell
}
func didSet( value: Int,for myStructIndex: Int ) {
// Now MyViewController sees the change.
myStructs[myStructIndex].value = value
}
}
最后,在MyCell中,每当值更改时(例如在步进器中),都调用:
@IBAction func stepperChanged( _ sender: UIStepper ) {
let integerValue = Int( sender.value.round() )
numLabel.text = "\(integerValue)"
// Tell the view controller about the change: what happened,and to what cell.
self.delegate.didSet( value: integerValue,for: self.myStructIndex )
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。