如何比较2个数组并将最后一个项添加到第二个数组中iOS Swift 3
假设我有一个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将滚动到顶部。
谢谢!
既然你问的方式追加它,这应该工作。
if firstArray.count > secondArray.count {
for i in secondArray.count..<firstArray.count {
secondArray.append(firstArray[i])
}
}
希望这有助于!
它确实, 非常感谢! – cubycode
如果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
}
如果您希望无需重新加载它来更新你的表视图,你将不得不使用 FUNC insertRows(在indexPaths:IndexPath]动画:UITableViewRowAnimation) 你应该看看这个文档为更好地理解如何实施你的任务: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html
是的,我知道如何将行添加到tableView,我只需要代码将最后一项添加到我的secondArray,就像下面的答案;) – cubycode
首先附加数据到第二阵列
secondArray.append(firstArray [指数])
然后
self.yourTableView.beginUpdates()
var insertedIndexPaths = [indexpath_where_you_want_insertData]
self.yourTableView.insertRowsAtIndexPaths(insertedIndexPaths, withRowAnimation: .Fade)
self.yourTableView.endUpdates()
如果您只是不想滚动到顶部,一种方法是在reloadData()后立即以编程方式滚动到底部,例如,用这个http://*.com/questions/952412/uiscrollview-scroll-to-bottom-programmatically – aunnnn