2017-02-28 41 views
0

假设我有一个PFObject [](来自Parse SDK)的firstArray,它有7个项目,我使用firstArray创建了第二个数组:如何比较2个数组并将最后一个项添加到第二个数组中iOS Swift 3

secondArray = firstArray 

然后我调用查询到数据库来检索更新的数据,他们现在是firstArray中的10个项目。

我做这样的事情:

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

我如何追加在其特定的顺序的最后3个项目到secondArray结束?

secondArray追加项目8

secondArray追加项目9

secondArray追加项目10

我不想reloadData(),我只是想添加最后3举例来说,像WhatsApp那样,我的TableView中有行,否则TableView将滚动到顶部。

谢谢!

+0

如果您只是不想滚动到顶部,一种方法是在reloadData()后立即以编程方式滚动到底部,例如,用这个http://stackoverflow.com/questions/952412/uiscrollview-scroll-to-bottom-programmatically – aunnnn

回答

1

既然你问的方式追加它,这应该工作。

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

希望这有助于!

+0

它确实, 非常感谢! – cubycode

1

如果firstArray和secondArray相同 你可以做

secondArray = firstArray 

,如果万一你已经更新secondArray并希望追加新的数据仅

var counter = 0 
while secondArray.count != firstArrayCount { 
    secondArray.append(firstArray[firstArray.count+counter]) 
    counter += 1 
    //insert new row in table 
} 
+0

是的,对不起,我没有正确表达我的问题,然后我需要更新我的TableView数据源而不重新加载所有数据,只添加最后3行到表的末尾,如WhatsApp所做的,我调用tableView.reloadData()它刷新整个TableView并将其滚动到顶部 – cubycode

+0

我已更新我的答案看看 – rv7284

0

如果您希望无需重新加载它来更新你的表视图,你将不得不使用 FUNC insertRows(在indexPaths:IndexPath]动画:UITableViewRowAnimation) 你应该看看这个文档为更好地理解如何实施你的任务: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html

+0

是的,我知道如何将行添加到tableView,我只需要代码将最后一项添加到我的secondArray,就像下面的答案;) – cubycode

1

首先附加数据到第二阵列

secondArray.append(firstArray [指数])

然后

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

self.yourTableView.insertRowsAtIndexPaths(insertedIndexPaths, withRowAnimation: .Fade) 
self.yourTableView.endUpdates() 
相关问题