I just tried to retrieve all my textfield values out of my TableView. It worked for 10 of 11 cases. I tried the following:
let journeyIDTextField = tableView.cellForRow(at: NSIndexPath(row: 0, section: 1) as IndexPath) as! InputTableViewCell
journeyID = journeyIDTextField.cellInputTextfield.text!
When I change the section from 1-10 everything works, section 0 results to an error. Fatal error: Unexpectedly found nil while unwrapping an Optional value
Therefore I tried to see if there is a textfield at IndexPath (0,0).
print(section.description, indexPath.row, indexPath.section)
Result: Description 0 0
So there is definitely a textfield at 0,0. I have no idea what to do, especially because it worked fine on another ViewController.
Any ideas?
Best, Timo
func numberOfSections(in tableView: UITableView) -> Int {
return JourneySection.allCases.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierInputCell, for: indexPath) as! InputTableViewCell
guard let section = JourneySection(rawValue: indexPath.section) else { return UITableViewCell() }
cell.cellInputTextfield.placeholder = section.description
print(section.description, indexPath.row, indexPath.section)
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
And this is my cell: import UIKit
class InputTableViewCell: UITableViewCell {
let cellInputTextfield: UITextField = {
let cellInputTextfield = UITextField()
cellInputTextfield.textColor = .black
cellInputTextfield.sizeToFit()
return cellInputTextfield
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .default, reuseIdentifier: reuseIdentifier)
cellInputTextfield.frame = CGRect(x: 20, y: 0, width: self.frame.width, height: 60)
cellInputTextfield.font = UIFont.systemFont(ofSize: 15)
self.contentView.addSubview(cellInputTextfield)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
enum JourneySection:Int, CaseIterable, CustomStringConvertible{
case Description
case ID
case Confirmation
case Destination
case DestinationDate
case DestinationTime
case Arrival
case ArrivalDate
case ArrivalTime
case PriceTotal
case Companions
var description: String {
switch self {
case .Description: return "Description"
case .ID: return "ID f.ex. Flight Number"
case .Confirmation: return "Confirmation No."
case .Destination: return "Destination"
case .DestinationDate: return "Destination Date, like DD-MM-YYYY"
case .DestinationTime: return "Destination Time, like hh-mm"
case .Arrival: return "Arrival"
case .ArrivalDate: return "Arrival Date, like DD-MM-YYYY"
case .ArrivalTime: return "Arrival Time, like hh-mm"
case .PriceTotal: return "Total Price"
case .Companions: return " No. Of Companions"
}
}
}