0

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!

Community
  • 1
  • 1
Ben
  • 3,346
  • 6
  • 32
  • 51
  • Static first cell: 1. create a property of type "CustomTableViewCell" make this custom cell contain a search bar. 2. initialize this cell your init methods, add this cell to a dictionary or not, then when you hit cell for row at index path" where row is the top row, return just this static cell, so use an if/then for this, it's all very simple – Larry Pickles Apr 26 '16 at 06:30

1 Answers1

0

Change this:

cell1.textLabel?.text = self.searchResultsGoogle[indexPath.row]

to:

cell1.textLabel?.text = self.searchResultsGoogle[indexPath.row - 1]

That will adjust the row index to take into account the fixed row.

A better solution might be to use separate sections. Put the fixed row in section 0 and your actual data in section 1.

BTW - you also have another issue due to cell reuse. You need to reset the text color for other rows. Otherwise as you scroll the table view, some of the rows will show the wrong color.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Yes! Thank you for sharing that beautiful brain of yours. Good point about sections in tables. I need to learn more about that to feel comfortable using that. FYI, for the font color, I intentionally want the static cell to be a different color so it stands out. Thanks for calling it out none-the-less! – Ben Apr 26 '16 at 14:08
  • You missed my point about the cell color. It's fine to use a different color for the static cell. But you also need to reset the color of the non-static cells. If you don't, you will see the color of the static cell appear in places you don't want it as the user scrolls the table view. – rmaddy Apr 26 '16 at 14:18
  • Got it. Thanks for clarifying! – Ben Apr 27 '16 at 14:48