import UIKit class ViewController: UIViewController { // MARK: - Properties @IBOutlet var Tableview: UITableView! let searchController = UISearchController(searchResultsController: nil) struct Cable { let id: String let name: String let description: String let insulationColor: String let insulationMaterial: String let outerSheathColor: String let outerSheathMaterial: String let outerDescription: String init(id: String, name: String, description: String, insulationColor: String, insulationMaterial: String, outerSheathColor: String, outerSheathMaterial: String, outerDescription: String) { self.id = id self.name = name self.description = description self.insulationColor = insulationColor self.insulationMaterial = insulationMaterial self.outerSheathColor = outerSheathColor self.outerSheathMaterial = outerSheathMaterial self.outerDescription = outerDescription } } var cables = [Cable]() var filteredCables = [Cable]() var isSearchBarEmpty: Bool { return searchController.searchBar.text?.isEmpty ?? true } var isFiltering: Bool { return searchController.isActive && !isSearchBarEmpty } // MARK: - Outlets @IBOutlet weak var tableView: UITableView! @IBOutlet weak var addButton: UIBarButtonItem! // MARK: - View Lifecycle override func viewDidLoad() { super.viewDidLoad() // Connect the table view to the data source and delegate Tableview.dataSource = self Tableview.delegate = self // Register the table view cell Tableview.register(UITableViewCell.self, forCellReuseIdentifier: "CableCell") // Set up the search controller searchController.searchResultsUpdater = self searchController.obscuresBackgroundDuringPresentation = false searchController.searchBar.placeholder = "Search Cables" searchController.searchBar.delegate = self navigationItem.searchController = searchController definesPresentationContext = true Tableview.tableHeaderView = searchController.searchBar // Load the list of cables loadCables() } private func loadCables() { // Load the cables from a file or network request cables = [Cable(id: "Cable1", name: "HDMI Cable", description: "A cable used to connect audio and video devices.", insulationColor: "White", insulationMaterial: "Plastic", outerSheathColor: "Black", outerSheathMaterial: "Plastic", outerDescription: "Standard HDMI cable."), Cable(id: "Cable2", name: "USB Cable", description: "A cable used to connect devices to a computer.", insulationColor: "Black", insulationMaterial: "Rubber", outerSheathColor: "White", outerSheathMaterial: "Plastic", outerDescription: "Standard USB cable.")] self.Tableview.reloadData() } } // MARK: - UITableViewDataSource, UITableViewDelegate extension ViewController: UITableViewDataSource, UITableViewDelegate,UISearchBarDelegate, UISearchResultsUpdating{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if isFiltering { return filteredCables.count } return cables.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "CableCell", for: indexPath) let cable: Cable if isFiltering { cable = filteredCables[indexPath.row] } else { cable = cables[indexPath.row] } cell.textLabel?.text = cable.name cell.detailTextLabel?.text = cable.description return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // Show an alert with the cable information let cable = cables[indexPath.row] let alert = UIAlertController(title: cable.name, message: "ID: (cable.id) \n Description: (cable.description) \n Insulation: (cable.insulationColor) (cable.insulationMaterial) \n Outer Sheath: (cable.outerSheathColor) (cable.outerSheathMaterial) \n Outer Description: (cable.outerDescription)", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) present(alert, animated: true, completion: nil) } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { filterContentForSearchText(searchText) } func updateSearchResults(for searchController: UISearchController) { let searchBar = searchController.searchBar filterContentForSearchText(searchBar.text!) } } // MARK: - Private Methods extension ViewController { private func filterContentForSearchText(_ searchText: String) { filteredCables = cables.filter { (cable: Cable) -> Bool in return cable.name.lowercased().contains(searchText.lowercased()) } Tableview.reloadData() } } // MARK: - IBActions extension ViewController { @IBAction func addButtonTapped(_ sender: Any) { // Show an alert to add a new cable let alert = UIAlertController(title: "Add Cable", message: "Enter the information for the new cable:", preferredStyle: .alert) alert.addTextField { (textField) in textField.placeholder = "ID" } alert.addTextField { (textField) in textField.placeholder = "Name" } alert.addTextField { (textField) in textField.placeholder = "Description" } alert.addTextField { (textField) in textField.placeholder = "Insulation Color" } alert.addTextField { (textField) in textField.placeholder = "Insulation Material" } alert.addTextField { (textField) in textField.placeholder = "Outer Sheath Color" } alert.addTextField { (textField) in textField.placeholder = "Outer Sheath Material" } alert.addTextField { (textField) in textField.placeholder = "Cable Outer Description" } let addAction = UIAlertAction(title: "Add", style: .default) { [weak self] (_) in let idTextField = alert.textFields![0] as UITextField let nameTextField = alert.textFields![1] as UITextField let descriptionTextField = alert.textFields![2] as UITextField let insulationColorTextField = alert.textFields![3] as UITextField let insulationMaterialTextField = alert.textFields![4] as UITextField let outerSheathColorTextField = alert.textFields![5] as UITextField let outerSheathMaterialTextField = alert.textFields![6] as UITextField let outerDescriptionTextField = alert.textFields![7] as UITextField guard let id = idTextField.text, !id.isEmpty, let name = nameTextField.text, !name.isEmpty, let description = descriptionTextField.text, !description.isEmpty, let insulationColor = insulationColorTextField.text, !insulationColor.isEmpty, let insulationMaterial = insulationMaterialTextField.text, !insulationMaterial.isEmpty, let outerSheathColor = outerSheathColorTextField.text, !outerSheathColor.isEmpty, let outerSheathMaterial = outerSheathMaterialTextField.text, !outerSheathMaterial.isEmpty, let outerDescription = outerDescriptionTextField.text, !outerDescription.isEmpty else { return } let newCable = Cable(id: UUID().uuidString, name: name, description: description, insulationColor: insulationColor, insulationMaterial: insulationMaterial, outerSheathColor: outerSheathColor, outerSheathMaterial: outerSheathMaterial, outerDescription: outerDescription) self?.cables.append(newCable) self?.Tableview.reloadData() } alert.addAction(addAction) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancelAction) present(alert, animated: true, completion: nil) } }