0

Let's say i have a firstArray of PFObject[](from Parse SDK), and it has 7 items, i make a secondArray out of firstArray:

secondArray = firstArray

then i call a query to the database to retrieve updated data, they are now 10 items in firstArray.

I do something like this:

if firstArray.count > secondArray.count {
      let lastItems = firstArray.count - secondArray.count
     // lastItems = 3
}

How do i append those 3 last items to the end of the secondArray in their specific order?

secondArray append item 8

secondArray append item 9

secondArray append item 10

I don't want to reloadData(), i just want to add the last 3 rows to my TableView like WhatsApp does, for example, otherwise the TableView will scroll to the top.

Thanks!

Community
  • 1
  • 1
Frank Eno
  • 2,581
  • 2
  • 31
  • 54
  • If you just don't want to scroll to the top, one way is to programmatically scroll to bottom immediately after reloadData(), e.g. with this http://stackoverflow.com/questions/952412/uiscrollview-scroll-to-bottom-programmatically – aunnnn Feb 28 '17 at 07:20

4 Answers4

2

First append data to second array

secondArray.append(firstArray[index])

Then

self.yourTableView.beginUpdates()
var insertedIndexPaths = [indexpath_where_you_want_insertData]

self.yourTableView.insertRowsAtIndexPaths(insertedIndexPaths, withRowAnimation: .Fade)
self.yourTableView.endUpdates()
Punit
  • 1,330
  • 6
  • 13
1

if firstArray and secondArray are same you can just do

secondArray = firstArray

and if in case you have updated secondArray and wants to append new data only

var counter = 0
while secondArray.count != firstArrayCount {
    secondArray.append(firstArray[firstArray.count+counter])
    counter += 1
    //insert new row in table
}
rv7284
  • 1,092
  • 9
  • 25
  • Yes, sorry, I haven't expressed my question correctly, i then need to update my TableView Datasource without reloading all data, only adding the last 3 rows to the end of the table, like WhatsApp does for example, of I call tableView.reloadData() it refreshes the entire TableView and scrolls it to the top – Frank Eno Feb 28 '17 at 07:00
1

Since you ask for a way to append it, this should work.

if firstArray.count > secondArray.count {
    for i in secondArray.count..<firstArray.count {
        secondArray.append(firstArray[i])
    }
}

Hope this helps!

GerardoMR
  • 81
  • 3
0

If you wish to update your table view without reloading it, you'll have to use func insertRows(at indexPaths: [IndexPath], with animation: UITableViewRowAnimation) You should look at this doc for better understanding of how to implement your task: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html

Eugene Svaro
  • 394
  • 2
  • 5
  • Yes, I know how to add rows to a tableView, i just needed the code to append last items to my secondArray, like the answer below this one ;) – Frank Eno Feb 28 '17 at 07:37