Вместо использования двух табличных представлений используйте одно табличное представление с двумя разделами.И не используйте 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! }
}
}
}