将引用存储在变量 SWIFT 4
store reference in variable SWIFT 4
我从 viewController 向 customCell 发送了一个参数。我的自定义单元格有一个文本字段。在我的 viewController 中,我有一个字符串变量用于将值存储在 textField 中,因为我将把这个值发送到休息服务。
MyCustomCell
customCell: TableViewCell: {
var data: String?
@IBOutlet weak var textField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
}
func setData(data: inout String) {
self.data = data
}
}
extension customCell: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
data = textField.text ?? ""
}
}
MyViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! customCell
cell.setData(data: &data.email)
return cell
}
当我将引用发送到 customCell 时,我在 textFieldDidEndEditing 方法中丢失了引用。
不要那样做。
使用 class
创建数据模型。 A class 是引用类型
class Model {
var name : String
var email : String
init(name: String, email: String) {
self.name = name
self.email = email
}
}
声明数据源数组
var models = [Model]()
在单元格中声明一个 属性 模型(顺便说一下 class 名称以大写字母开头)
class CustomCell: TableViewCell {
@IBOutlet weak var textField: UITextField!
var model: Model! {
didSet {
textField.text = model.email
}
}
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
}
}
extension CustomCell: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
model.email = textField.text ?? ""
}
}
在 cellForRow 中将模型传递给单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell
cell.model = models[indexPath.row]
return cell
}
由于引用语义,模型中的值得以保留。
以下内容并非如您所想:
func setData(data: inout String) {
self.data = data
}
当然,您传递给此方法的 String
是 inout
,但这不会改变您单元格的 String
属性 是值类型的事实因此,您的 self.data
实际上变成了一个新实例。
从根本上说,您正在尝试使用 String
实现引用类型语义,这是一种值类型。
你可以通过扭曲自己来实现这一点,但我不鼓励你这样做。为单元格采用闭包或 protocol-delegate 或反应模式来通知视图控制器或模型更改会更清晰。
我从 viewController 向 customCell 发送了一个参数。我的自定义单元格有一个文本字段。在我的 viewController 中,我有一个字符串变量用于将值存储在 textField 中,因为我将把这个值发送到休息服务。
MyCustomCell
customCell: TableViewCell: {
var data: String?
@IBOutlet weak var textField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
}
func setData(data: inout String) {
self.data = data
}
}
extension customCell: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
data = textField.text ?? ""
}
}
MyViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! customCell
cell.setData(data: &data.email)
return cell
}
当我将引用发送到 customCell 时,我在 textFieldDidEndEditing 方法中丢失了引用。
不要那样做。
使用 class
创建数据模型。 A class 是引用类型
class Model {
var name : String
var email : String
init(name: String, email: String) {
self.name = name
self.email = email
}
}
声明数据源数组
var models = [Model]()
在单元格中声明一个 属性 模型(顺便说一下 class 名称以大写字母开头)
class CustomCell: TableViewCell {
@IBOutlet weak var textField: UITextField!
var model: Model! {
didSet {
textField.text = model.email
}
}
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
}
}
extension CustomCell: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
model.email = textField.text ?? ""
}
}
在 cellForRow 中将模型传递给单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell
cell.model = models[indexPath.row]
return cell
}
由于引用语义,模型中的值得以保留。
以下内容并非如您所想:
func setData(data: inout String) {
self.data = data
}
当然,您传递给此方法的 String
是 inout
,但这不会改变您单元格的 String
属性 是值类型的事实因此,您的 self.data
实际上变成了一个新实例。
从根本上说,您正在尝试使用 String
实现引用类型语义,这是一种值类型。
你可以通过扭曲自己来实现这一点,但我不鼓励你这样做。为单元格采用闭包或 protocol-delegate 或反应模式来通知视图控制器或模型更改会更清晰。