简体   繁体   中英

How to compare 2 arrays and append last items to second array iOS Swift 3

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!

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

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
}

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!

如果您希望不重新加载就更新表格视图,则必须使用func insertRows(在indexPaths:[IndexPath],动画:UITableViewRowAnimation)。您应该查看此文档,以更好地了解如何实现任务: https ://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM