I am trying to figure out how to have a static first cell in a UITableView and then have dynamic results for the rest of the cells.
More specifically, I am using the Google SDK and when the user types in an address or location into a UISearchBar then results autocomplete in a UITableView below. I want the first cell under the Search Bar to remain static and say "Current Location" and then the autocomplete results underneath that.
I tried using an approach similar to what's outlined here. I can't figure out how to make it work because this makes the index returned from Google autocomplete out of range.
Here's the general approach that I've been trying that didn't work:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.searchResultsGoogle.count + 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath)
if indexPath.row == 0 {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath)
cell.textLabel?.text = "Current Location"
cell.textLabel?.textColor = UIColor(red: 14/255, green: 122/255, blue: 254/255, alpha: 1)
return cell
}
else {
let cell1 = self.tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath)
cell1.textLabel?.text = self.searchResultsGoogle[indexPath.row]
return cell1
}
}
Any ideas on how to fix this? Thanks!