sorting UITableView into sections
<p>I'm new to swift &amp; Xcode (teaching myself via tutorials and stackoverflow). I've written some code that adds places to a list in a TableView and now I am trying to sort that list of places into sections. </p>

<p>Specifically, I have a ViewController where I input name, neighborhood and friend (all strings) and this adds a Place to the bottom of my TableView. </p>

<p>I want to group this list of places by neighborhood, and display all the places in the same neighborhood together in a section, using the neighborhood string as the section header.</p>

<p>I'm close, but I'm not indexing my sections correctly. indexPath.section I believe is what I'm missing..</p>

<p>So far I have this code in my TableViewController:</p>

<pre><code>// MARK: Properties

var places = [Place]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Use the edit button item provided by the table view controller.
    navigationItem.leftBarButtonItem = editButtonItem()

    // Load any saved places, otherwise load sample data.
    if let savedPlaces = loadPlaces() {
        places += savedPlaces
    }

    else {
        // Load the sample data.
        loadSamplePlaces()
    }

    // Sort by neighborhood
    places.sortInPlace { (place1, place2) -&gt; Bool in
        return place1.neighborhood &lt; place2.neighborhood
    }
}

// MARK: Getting Count, Number and Name of Neighborhoods
func getCountForNeighborhood(neighborhood:String) -&gt; Int {
    return places.filter { (place) -&gt; Bool in
        return place.neighborhood == neighborhood
    }.count
}

func getNumberOfNeighborhoods() -&gt; Int {
    var neighborhoodCount = 0
    var neighborhoodPrev = "Not a real neighborhood"
    for place in places {
        if place.neighborhood != neighborhoodPrev {
            neighborhoodCount += 1
        }
        neighborhoodPrev = place.neighborhood
    }
    return neighborhoodCount
}

func getNeighborhoodForSection(section:Int) -&gt; String {
    var previousNeighborhood:String = "Not a real neighborhood"
    var currentIndex = -1

    for place in places {

        if(place.neighborhood != previousNeighborhood) {
            currentIndex += 1
        }
        if(currentIndex == section){
            return place.neighborhood
        }
        previousNeighborhood = place.neighborhood
    }

    return "Unknown"
}

func loadSamplePlaces() {

    let place1 = Place(name: "Motorino", neighborhood: "East Village", friend: "Maggles")!
    let place2 = Place(name: "Bar Primi", neighborhood: "Lower East Side", friend: "Em")!
    let place3 = Place(name: "El Carino", neighborhood: "Williamsburg", friend: "Ruby")!

    places += [place1, place2, place3]
}

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


// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
    //alter this for To Try, Been To sections =2
    return getNumberOfNeighborhoods()
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
    // #warning Incomplete implementation, return the number of rows

    let neighborhood = getNeighborhoodForSection(section)
    return getCountForNeighborhood(neighborhood)
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
    // Table view cells are reused and should be dequeued using a cell identifier.
    let cellIdentifier = "PlaceTableViewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! PlaceTableViewCell

    // Fetches the appropriate place for the data source layout.

    let place = places[indexPath.row]

    cell.placeLabel.text = place.name
    cell.neighborhoodLabel.text = place.neighborhood
    cell.friendLabel.text = place.friend

    return cell
}

//Add section headers
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {
    return getNeighborhoodForSection(section)

}
</code></pre>

<p>I'm fairly certain it's an issue with my cellForRowAtIndexPath method. I think I'm missing some code to properly index the data into sections...</p>

<p>Current build displays <img src="https://i.stack.imgur.com/w8HXEm.png" alt="this"></p>