【发布时间】:2021-01-27 09:56:58
【问题描述】:
我想在表格视图的动态单元格中读取文本字段中的文本。
每个单元格有两个文本字段,用户可以添加更多单元格。
我的问题: 如何从文本字段(textField1 和 textField2)中读取值并将它们添加到我的卡片结构中?
在 Cards 结构中有两个变量。 frontLabel 和 backLabel 作为字符串
我的表视图:
class AddFCViewController: UIViewController {
var numberOfTextFields = 1
var cards = [Cards]()
// Table View
let tableView : UITableView = {
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.backgroundColor = .purple
return tableView
}()
// Add Button
let addButtton : UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.backgroundColor = .link
btn.setTitle("Ekle", for: .normal)
return btn
}()
override func viewDidLoad() {
super.viewDidLoad()
configureTableView()
configureAddButton()
}
// MARK: - Configure Add Button
func configureAddButton() {
view.addSubview(addButtton)
addButtton.addTarget(self, action: #selector(addButtonTapped(sender:)), for: .touchUpInside)
}
// MARK: - Add Button Tapped MEthod
@objc func addButtonTapped(sender: UIButton){
numberOfTextFields += 1
tableView.reloadData()
}
// MARK: - Configure Table View
func configureTableView() {
view.addSubview(tableView)
setTableViewDelegates()
tableView.rowHeight = 100
tableView.register(AddFCCell.self, forCellReuseIdentifier: "AddFCCell")
}
// MARK: - Table View Delegates
func setTableViewDelegates() {
tableView.delegate = self
tableView.dataSource = self
}
}
// MARK: - Table View Extension & Delegate Methods
extension AddFCViewController: UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfTextFields
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "AddFCCell") as! AddFCCell
cell.selectionStyle = .none
cell.textField1.tag = indexPath.row
cell.textField2.tag = indexPath.row
print(cell.textField1.tag)
print(cell.textField2.tag)
// cell.textField1.text = cards[indexPath.row].frontLabel
// cell.textField2.text = cards[indexPath.row].backLabel
cell.textField1.delegate = self
cell.backgroundColor = .purple
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 10
}
func textFieldDidBeginEditing(_ textField: UITextField) {
// textField.addTarget(self, action: #selector(valueChanged), for: .editingChanged)
}
}
这是我的自定义单元类。
class AddFCCell: UITableViewCell {
// MARK: - Text Field 1
let textField1 : UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.backgroundColor = .white
textField.isUserInteractionEnabled = true
textField.placeholder = "TYPE HERE SOMETHING"
textField.font = UIFont.systemFont(ofSize: 20)
return textField
}()
// MARK: - Text Field 2
let textField2 : UITextField = {
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.backgroundColor = .white
textField.isUserInteractionEnabled = true
textField.placeholder = "TYPE HERE SOMETHING"
textField.font = UIFont.systemFont(ofSize: 20)
return textField
}()
// MARK: - view Did Load
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(textField1)
contentView.addSubview(textField2)
configureTextBox1(textField: textField1)
configureTextBox2(textField: textField2)
}
// MARK: - Configure Text Box 1 Function
func configureTextBox1(textField : UITextField) {
textField.layer.cornerRadius = 20
}
// MARK: - Configure Text Box 2 Function
func configureTextBox2(textField : UITextField) {
textField.layer.cornerRadius = 20
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
PS - 我删除了一些约束代码,因为那里的代码太多了。
【问题讨论】:
-
"我如何从文本字段中读取值"什么时候?当用户点击某个特定按钮或用户停止编辑文本字段时?
-
抱歉,缺少信息。当用户完成编辑后,我想读取数据。
标签: swift tableview swift5 custom-cell