0

I am making a search bar in Xcode, and have added tableViewCells. When I am not searching and click on the tableViewCells, I will be guided to a viewController displaying more specific information. But when I actually search something on the search bar, and click on the tableViewCell, there is no viewController. I am not sure where I went wrong; I am an amateur, though.

import UIKit

var roomies = [Room]()
var text = ["1st Floor of the Upper Building","2nd Floor of the Upper 
Building","3rd Floor of the Upper Building","4th Floor of the Upper 
Building","5th Floor of the Upper Building","6th Floor of the Upper 
Building","1st Floor of the Intermediate Building","2nd Floor of the 
Intermediate Building","1st Floor of the Lower Building","2nd Floor of the 
Lower Building","3rd Floor of the Lower Building","4th Floor of the Lower 
Building","1st Floor of the Rittmann","2nd Floor of the Rittmann","3rd Floor 
of the Rittmann","4th Floor of the Rittmann","1st Floor of the Phoenix 
Center","2nd Floor of the Phoenix Center"] 
var row = 0

class LocationSearch: UITableViewController, UISearchResultsUpdating{




var filtered = [Room]()
var searchController : UISearchController!
var resultsController = UITableViewController()

override func viewDidLoad() {
    super.viewDidLoad()
    roomies = [
        Room(roomName:"H316/H318, Wittenberg Hall", floorNumber: 2),
        Room(roomName:"PC Lounge", floorNumber:2),
        Room(roomName:"H115, HS Gym, ECOSOC", floorNumber: 0),
        Room(roomName:"P228, Phoenix Café", floorNumber: 1),
        Room(roomName:"H227, HS Library, Approval Panel", floorNumber: 1),
        Room(roomName: "H233, HS Conference Room", floorNumber: 1),
        Room(roomName:"H405/H406, Augsburg Hall, Environmental Committee", floorNumber: 3),
        Room(roomName:"P106, PC Gym", floorNumber: 16),
        Room(roomName:"M116, MS Gym", floorNumber: 7),
        Room(roomName:"E322, ES Library, HRC", floorNumber: 10),
        Room(roomName:"E122, ES Cafeteria", floorNumber: 8),
        Room(roomName:"R302, Orchestra Room", floorNumber: 14),
        Room(roomName:"PC Commons", floorNumber: 16),
        Room(roomName:"H116, Old Luther, ICJ", floorNumber: 0),
        Room(roomName:"P230/P231, Jade/Pearl, CSW", floorNumber: 17),
        Room(roomName:"R110, Costume Room, Secretariat/Hive", floorNumber: 12),
        Room(roomName:"H127, Health Room", floorNumber: 0),
        Room(roomName:"R101, Rittmann Theatre", floorNumber: 12),
        Room(roomName:"H305, CISSMUN Vigil", floorNumber: 2),
        Room(roomName:"H404, Spec Conf", floorNumber: 3),
        Room(roomName:"H403, UNPFII", floorNumber:3),
        Room(roomName:"P201, New Luther, ICC", floorNumber: 17),
        Room(roomName:"H205, Printing", floorNumber: 1),
        Room(roomName:"P106, Phoenix Center Gym, Keynote Speakers", floorNumber:16),
        Room(roomName:"Classroom next to Jade/Pearl???", floorNumber: 17),
        Room(roomName:"H203", floorNumber:1),
        Room(roomName:"H204", floorNumber:1),
        Room(roomName:"H206", floorNumber:1),
        Room(roomName:"MS and HS classrooms Schools???", floorNumber:0),
        Room(roomName:"MSR???",floorNumber:6)
    ]


    searchController = UISearchController(searchResultsController: resultsController)
    tableView.tableHeaderView = searchController.searchBar
    searchController.searchResultsUpdater = self
    resultsController.tableView.dataSource = self
    resultsController.tableView.delegate = self
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    searchController.hidesNavigationBarDuringPresentation = false
    //searchController.searchBar.barTintColor = UIColor.black
}



func updateSearchResults(for searchController: UISearchController) {
    filtered = roomies.filter({(room:Room) -> Bool in
         return room.roomName.lowercased().contains(self.searchController.searchBar.text!.lowercased())

    })
    resultsController.tableView.reloadData()
}



// MARK: - Table view data source
    override  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if tableView == resultsController.tableView{
        return filtered.count
    }
    else {
        return roomies.count
    }

}

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if tableView == resultsController.tableView{
         let cell = UITableViewCell()
        cell.textLabel?.text = filtered[indexPath.row].roomName
        return cell
    }
    else {
         let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
        cell.textLabel?.text = roomies[indexPath.row].roomName
        return cell
    }

}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    row = indexPath.row
}
/*
func tableView(_ tableView: UITableView, didSelectRow indexPath: IndexPath) {

}
*/
}

Above is my code for the tableViewCells. Below is the code for the ViewController that connects to the tableViewCell. import UIKit

class CellsViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var scroll: UIScrollView!
@IBOutlet weak var textView: UILabel!
@IBOutlet weak var textViewtwo: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    imageView.image = images[(NSInteger)(roomies[row].floorNumber)]
    scroll.delegate = self
    textView.text = roomies[row].roomName
    textViewtwo.text = "Located on the " + text[ (NSInteger)(roomies[row].floorNumber)  ]
    //[@"7" intValue];
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return imageView
}



}

Is there any way to easily add a ViewController to the filtered tableViewCells? All help is appreciated.

haost
  • 1
  • 1
  • How are you currently segueing when you select a cell? It looks like your didSelectRowAt isn't doing that so where is that code? – DoesData Nov 21 '17 at 12:28
  • I'm segueing through Main.storyboard. Do I need to add code for the segue to work correctly? – haost Nov 21 '17 at 12:55

1 Answers1

0

Create a new view controller and add a segue to in. You can look at the documentation or this question for help if need be.

Then you can call your segue in didSelectRowAt

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if tableView == resultsController.tableView{
        DispatchQueue.main.async {
            self.performSegue(withIdentifier: "toWhereYouWantWhenFilterIsActive", sender: nil)
        }
    }
    else {
        DispatchQueue.main.async {
            self.performSegue(withIdentifier: "toWhereYouWantWhenNotFiltered", sender: nil)
        }
    }
}
DoesData
  • 6,594
  • 3
  • 39
  • 62