【发布时间】:2020-09-09 17:18:32
【问题描述】:
我有一个自定义单元格,它有 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
}
}
【问题讨论】:
标签: ios swift uitableview uikit