我有两个具有类似单元格设计的tableView。单元格名称和年龄里面有两个textField。您可以在图像中看到。
现在用户将数据填充到每个textField中。然后在continue
按钮上,我想在数组上附加所有名称。年龄相似。
并且还想将验证值作为空白值。如果任何文本字段为空白,请按Continue
按钮,弹出警报。
答案 0 :(得分:0)
您可以使用方法cellForRow(at:)获取表格视图的单元格,然后按如下所示相应地访问名称标签或年龄标签
let indexPath = IndexPath(row: 0, section: 0)
if let cell = yourTableView.cellForRow(at: indexPath) as? yourTableViewCell {
let name = cell.nameLabel.text
...
}
答案 1 :(得分:0)
需要在UITextFieldDelegate
中实现ViewController
并实现TextFieldEndEditing
方法,然后将编辑后的文本字段文本保存在字符串数组中。
请参考以下链接以获取所有文本字段值。
https://phrkrish.wordpress.com/2016/12/25/table-view-with-text-field-in-swift-3-0/
答案 2 :(得分:0)
不是使用两个表视图,而是将一个表视图包含两个部分。并且不要使用dequeueReusableCell。创建两个UITableViewCell对象数组。并通过继续按钮操作从数组对象中获取文本字段值。
// CustomCell.swift
class CustomCell: UITableViewCell {
let titleLbl = UILabel()
let seatNoLbl = UILabel()
let nameTxtField = UITextField()
let ageTxtField = UITextField()
//etc
}
// ViewController.swift
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView()
var onwardSeatNos = ["L1","L15"]
var returnSeatNos = ["L2","L3"]
var onwardSeatCells = [CustomCell]()
var returnSeatCells = [CustomCell]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
onwardSeatNos.enumerated().forEach { (index,seatNo) in
let cell = CustomCell()
print(index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index))
cell.titleLbl.text = index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)
cell.seatNoLbl.text = "Seat No. \(seatNo)"
onwardSeatCells.append(cell)
}
returnSeatNos.enumerated().forEach { (index,seatNo) in
let cell = CustomCell()
cell.titleLbl.text = index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)
cell.seatNoLbl.text = "Seat No. \(seatNo)"
returnSeatCells.append(cell)
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return returnSeatNos.isEmpty ? 1 : 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return onwardSeatNos.count
} else {
return returnSeatNos.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
return onwardSeatCells[indexPath.row]
} else {
return returnSeatCells[indexPath.row]
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Passenger Information (Onward)"
} else {
return "Passenger Information (Return)"
}
}
@objc func confirmBtnAction(_ sender: UIButton) {
if let index = onwardSeatCells.firstIndex(where: { $0.nameTxtField.text!.isEmpty }) {
print("Onward " + (index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)) + " name is empty")
} else if let index = onwardSeatCells.firstIndex(where: { $0.nameTxtField.text!.isEmpty }) {
print("Onward " + (index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)) + " age is empty")
} else if let index = returnSeatCells.firstIndex(where: { $0.nameTxtField.text!.isEmpty }) {
print("Return " + (index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)) + " name is empty")
} else if let index = returnSeatCells.firstIndex(where: { $0.nameTxtField.text!.isEmpty }) {
print("Return " + (index == 0 ? "Primary Passenger" : String(format: "Co-passenger %02d", index)) + " age is empty")
} else {
let onwardNamesArr = onwardSeatCells.map { $0.nameTxtField.text! }
let onwardAgesArr = onwardSeatCells.map { $0.ageTxtField.text! }
let returnNamesArr = returnSeatCells.map { $0.nameTxtField.text! }
let returnAgesArr = returnSeatCells.map { $0.ageTxtField.text! }
}
}
}