Hi,
Sorry to be bothering you all with this but im quite new to coding and i just cant get this work. I have been able to run the code with no errors but i am meeting issues when i Run the code and the "iphone" screen turn up all white.
I have been using Table View Controller Scene and put it as "Initial View Controller", but when i Run it i still just get a white screen. I believe i might have some issues when it comes to using capital letters some where and some not, but im not sure tbh if that is the root cause.
If you have time to come with some feedback that would be greatly appreciated and if not still greatly appreciated that you took a look.
(and sorry for the messy writing)
The whole code:
Part of the code:
`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()
}
}