2

I'm trying to sort and section a UTableView with a Custom Cell Alphabetically. In my ViewController I have a function that downloads the JSON data then stores it in a Dictionary.

var attendees = [Attendee]()
var sortedFirstLetters: [String] = []
var sections: [[Attendee]] = [[]]

func downloadAttendeeData(completed: @escaping DownloadComplete) {
    let headers: HTTPHeaders = [

        "Accept": "application/json;odata=verbose",
        "User-Agent": "iOS;iOS"

    ]

    NetworkManager.sharedInstance.manager.request(Attendee_URL, method: .get, headers: headers)
        .responseJSON { response in

            let result = response.result

            if let dict = result.value as? Dictionary<String, AnyObject> {

                if let list = dict["d"]?["results"] as? [Dictionary<String, AnyObject>] {

                    for att in list {
                        let attendee = Attendee(AttendeeDict: att)
                        self.attendees.append(attendee)

                    }


                    self.tableView.reloadData()

                }

            }

            completed()
    }


}

This piece of code is exactly what I need

    let firstLetters = self.attendees.map { $0.titleFirstLetter }
    let uniqueFirstLetters = Array(Set(firstLetters))
    self.sortedFirstLetters = uniqueFirstLetters.sorted()

    self.sections = self.sortedFirstLetters.map { firstLetter in
        return self.names
            .filter { $0.titleFirstLetter == firstLetter }
            .sorted { $0.titleAD < $1.titleAD }
    }

However when I put this code in viewDidLoad it produces and Empty Array.

override func viewDidLoad() {
    super.viewDidLoad()


    tableView.delegate = self
    tableView.dataSource = self
    searchBar.delegate = self

    let firstLetters = self.attendees.map { $0.titleFirstLetter }
    let uniqueFirstLetters = Array(Set(firstLetters))
    self.sortedFirstLetters = uniqueFirstLetters.sorted()

    self.sections = self.sortedFirstLetters.map { firstLetter in
        return self.names
            .filter { $0.titleFirstLetter == firstLetter }
            .sorted { $0.titleAD < $1.titleAD }
    }

    self.downloadAttendeeData {


    }



}

If I put the code inside the self.downloadAttendeeData declaration it produces data but it doesn't create the table sections.

How can I produce the sections after the data is loaded into the tableView? Is that the problem?

user1938745
  • 193
  • 2
  • 5
  • 16
  • Can you put the sorted array in `titleForHeaderInSection:` – Vini App Sep 06 '17 at 01:19
  • Unfortunately that produces an empty table . – user1938745 Sep 06 '17 at 06:09
  • 2
    A few ideas... #1 - Are you properly overriding the numberOfSectionsInTableView? #2 - When all the data is supposedly downloaded and sectioned, print the content of the data structures to ensure that they are actually being built properly #3- When all the data is supposedly downloaded and sectioned, call tableView.reloadData(). To ensure that this is not some sort of timing issue #4- Make sure that you call tableView.reloadData() from the main thread: dispatch_async(dispatch_get_main_queue()) { self.tableView.reloadData() } – infinite-loop Sep 07 '17 at 20:58
  • @infinite-loop calling the tableView.reload from the main thread was the trick!! Thx – user1938745 Sep 12 '17 at 03:20

1 Answers1

2

Make sure that you call tableView.reloadData() from the main thread like this:

dispatch_async(dispatch_get_main_queue()) { 
   self.tableView.reloadData()
}

or in Swift 3 and later:

DispatchQueue.main.async {
  self.tableView.reloadData()
}
Míng
  • 2,500
  • 6
  • 32
  • 48
infinite-loop
  • 882
  • 11
  • 17