Reputation: 618
I have a table view populated by Realm Objects, and when i click on a cell, it takes me to another view that shows me more information about that Object. I want to add a search bar for this table view to check for the objects' "fullName" property. I know how to add a search bar for an array of strings, but I haven't found any tutorial or guidance on how to do it using Swift 2.0. Any help please?
EDIT: That's how i'm populating my tableview
var dataSource : Results<Patient>!
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier: String = "myCell"
var cell = tableView.dequeueReusableCellWithIdentifier(identifier)
if cell == nil {
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: identifier)
}
let currentPatientInfo = dataSource[indexPath.section]
cell?.textLabel?.text = currentPatientInfo
cell?.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton
return cell!
}
I have the data in sections, not rows
EDIT: I added RealmSwiftSearchController
to my project but occasionally i get this error when i click on a cell :
fatal error: unexpectedly found nil while unwrapping an Optional value
and it points to this code :
extension RealmSearchViewController {
public override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
// The line below
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Object
// Line ends
self.resultsDelegate.searchViewController(self, willSelectObject: object, atIndexPath: indexPath)
return indexPath
}
Upvotes: 1
Views: 2217
Reputation: 15991
There's an open-source component called RealmSearchViewController
that should be able to take care of pretty much all of the boilerplate code necessary with setting up a searchable UITableView
as well as interoperating it with Realm: https://github.com/bigfish24/ABFRealmSearchViewController
Additionally, there's a tutorial on how to implement it available on the Realm website: https://realm.io/news/building-an-ios-search-controller-in-swift/
Upvotes: 1