Here is the code:
class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
private let label: UILabel = {
let label = UILabel()
label.text = "Where to?"
label.font = .systemFont(ofSize: 24, weight: .semibold)
return label
}()
private let field: UITextField = {
let field = UITextField()
field.placeholder = "Enter destination"
field.layer.cornerRadius = 9
field.backgroundColor = .tertiarySystemBackground
field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 50))
field.leftViewMode = .always
return field
}()
private let tableView: UITableView = {
let table = UITableView()
table.register(UITableViewCell.self,
forCellReuseIdentifier: "cell")
return table
}()
var locations = [Location]()
override func viewDidLoad() {
view.backgroundColor = .secondarySystemBackground
view.addSubview(label)
view.addSubview(field)
view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
field.delegate
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
label.sizeToFit()
label.frame = CGRect(x: 10, y: 10, width: label.frame.size.width, height: label.frame.size.height)
field.frame = CGRect(x: 10, y: 20+label.frame.size.height, width: view.frame.size.width-20, height: 50)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
field.resignFirstResponder()
if let text = field.text, !text.isEmpty {
LocationMananger.shared.findLocation(with: text) { [weak self] location in
}
}
return true
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return locations.count
}
func tableview(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",
for: indexPath)
cell.textLabel?.text = locations[indexPath.row].title
cell.textLabel?.numberOfLines = 0
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let coordinate = locations[indexPath.row].coordinates
}
}
Please can you help, I'm realitivly new to swift and I need to finish this problem urgently.