Answer the question
In order to leave comments, you need to log in
How to make a form in UITableView?
Friends, I'm trying to make a form in UItableview. Several questions arose during the process. I'm not sure that I basically approached the issue from the right side.
There is a simple custom cell:
class CreateUpdateCell: UITableViewCell {
static let reuseID = "CreateUpdateCell"
let imageField = UIImageView()
let textField = UITextField()
...
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func set(image: UIImage, text: String) {
imageField.image = image
textField.placeholder = text
}
func configure() { ... }
}
extension ToolCreateVC: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CreateUpdateCell.reuseID, for: indexPath) as! CreateUpdateCell
cell.textField.tag = indexPath.row
switch indexPath.row {
case 0:
cell.set(image: UIImage(systemName: "pencil")!, text: "Наименование")
cell.textField.addTarget(self, action: #selector(textFieldValueChanged), for: .editingChanged)
case 1:
cell.set(image: UIImage(systemName: "tray")!, text: "Инвентарный номер")
cell.textField.addTarget(self, action: #selector(textFieldValueChanged), for: .editingChanged)
case 2:
cell.set(image: UIImage(systemName: "calendar")!, text: "Дата приобретения")
cell.textField.inputView = buyDateDatePicker
buyDateDatePicker.textField = cell.textField
cell.textField.addTarget(self, action: #selector(textFieldValueChanged), for: .editingChanged)
case 3:
cell.set(image: UIImage(systemName: "pencil")!, text: "Комментарий")
cell.textField.addTarget(self, action: #selector(textFieldValueChanged), for: .editingChanged)
default:
break
}
return cell
}
@objc func textFieldValueChanged(sender: UITextField) {
guard let value = sender.text else { return }
switch sender.tag {
case 0:
tool.name = value
case 1:
tool.codeNumber = value
case 2:
tool.buyDate = value.convertToyyyyMMddFormat()
case 3:
tool.comment = value
default:
break
}
}
}
class customUIDatePicker: UIDatePicker {
var textField = UITextField()
}
buyDateDatePicker.addTarget(self, action: #selector(dateChanged), for: .valueChanged)
cell.textField.inputView = buyDateDatePicker
buyDateDatePicker.textField = cell.textField
@objc func dateChanged() {
buyDateDatePicker.textField.text = buyDateDatePicker.date.convertToddMMyy()
tool.buyDate = buyDateDatePicker.textField.text?.convertToyyyyMMddFormat()
}
class ToolCreateVC: UIViewController {
//1:
var tool: Tool = Tool(id: 0, name: "", codeNumber: nil, buyDate: nil, comment: nil, toolmove: nil)
//2:
var tool2: Tool!
// Но тогда не могу обратиться к переменным, например: tool2.name
}
Answer the question
In order to leave comments, you need to log in
Good afternoon!
It's not entirely clear what you want to achieve. I so understand it what type of the table in which it is possible to change the data?
Then why dance with switch case? Make a model, in a cell through didSet processing and so on.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question