How to entries by recency in tableview?

Hi guys, hope you’re all safe. I’m adding a new feature in my app which also uses core data to allow user to snap photos or choose from their library so they can store them. Right now, when I add an entry it goes to the bottom of the tableview. I want my new entries to be on top. Any idea how to achieve this? Appreciate your time, thank you!

The first thing to do is insert the new item into your array at the 0 index (first position) of the array:

yourArray.insert(itemToAdd, at: 0)

Then the same item is inserted into the tableView by using this code:

let indexPath = IndexPath(row: 0, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)

That does so by animating the insertion so you see the existing first row move down and the new item replace it.

Thanks Chris! Not sure what to add in itemToAdd value, user enters image and title captured in core data. I tried your code in cell for row method but it wasn’t working. I’m sure I did it wrong. Below is cellForRow code.
> let cell = diaryTableView.dequeueReusableCell(withIdentifier: “diabetesDataCell”, for: indexPath) as!diabetesDataCell

        let diabetesUpdate = updates[indexPath.row]
        if let uploadedImage = diabetesUpdate.image{
            cell.dataImg?.image = UIImage(data: uploadedImage)
        cell.dataLbl.text = diabetesUpdate.title       
        return cell

Do you have a struct that defines the array updates?

Hi Chris, Thanks for your reply. I don’t have a struct. I have an array of core data objects in the view controller.

var updates:[DiabetesDiaryUpdate] = []
override func viewWillAppear(_ animated: Bool) {

    func updateCoreDataItems(){
        if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext{
           if let coreDataUpdateItems = try? context.fetch(DiabetesDiaryUpdate.fetchRequest()) as? [DiabetesDiaryUpdate]{
        updates = coreDataUpdateItems

Hi Chris,
I added more features to my GlucoTrak app, such as search bar, pinch zoom for image, and another core data feature allowing users to add images of their reports and recipes etc which they can store in one place. It’s approved. Thanks a lot for your time and help, appreciate it.