Patricks Patricks - 8 days ago 6
iOS Question

Reverse order in TableView with months

I have a problem that seems quite simple but I can't figure it out how to do it.
I have a simple table in which I want to show every month of the year. I did it like that:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 12
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell

let monthName = DateFormatter().monthSymbols[indexPath.row]
cell.dateLabel.text = monthName

return cell
}


Now the table starts with january on the top and so on but I want it just the other way round.

Answer

You can reverse the array like this

let monthName = DateFormatter().monthSymbols.reversed()[indexPath.row]