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!

1
  • 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 stackoverflow.com/questions/952412/… Commented Feb 28, 2017 at 7:20

4 Answers 4

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()
Sign up to request clarification or add additional context in comments.

Comments

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!

Comments

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
}

1 Comment

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
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

1 Comment

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 ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.