[英]Array & TableView : only add new items
I have a database and would like to make an app, that displays messages. 我有一个数据库,想创建一个显示消息的应用程序。
In my swift app there's a variable, let's fill it up with some random values 在我的swift应用中,有一个变量,让我们用一些随机值填充它
var globalMessages : var globalMessages :
{
["uid": 1, "msg": "test1"],
["uid": 2, "msg": "test2"],
["uid": 3, "msg": "test3"],
}
And I fetch these data from the net with urlSession: 我使用urlSession从网上获取这些数据:
{
["uid": 1, "msg": "test1"],
["uid": 10, "msg": "NEW"], //new data
["uid": 2, "msg": "test2"],
["uid": 3, "msg": "test3"]
}
Then I don't wanna reload all the table: I just want to add the ["uid": 10, "msg": "NEW"] new message to the table between the 1. and 2. user's messages, with a gentle animation pushing the two already-there messages. 然后,我不想重新加载所有表:我只想在用户消息1.和2.之间添加[[uid]:10,“ msg”:“ NEW”]新消息到表中。动画推送两个已经存在的消息。
Is it possible somehow? 有可能吗?
The question is which I can't figure out, that how can I check that which items are new in the new fetched data 's array, that are not in the old globalMessages , and if there is some then display it with animation. 问题是我不知道该怎么解决,该如何检查在新获取的数据数组中哪些是新项,而不是旧的globalMessages中的项 ,如果有,则用动画显示它们。
And if some item is no longer in the new fetched data , then remove it with animation from the tableview. 并且,如果某些项不再存在于新获取的数据中 ,请使用动画从表视图中将其删除。 ??
??
Currently I am doing this: 目前,我正在这样做:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return globalMessages.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BubbleCell", for: indexPath) as! BubbleCell
let bubble = globalMessages[indexPath.row]
cell.messageLabel.text = bubble.message
let typeColor = UIColor(rgb: 0xE2E2E2)
if let image = UIImage(named: "bubble") {
let h = image.size.height / 2
let w = image.size.width / 2
cell.bubbleImageView.image = image
.resizableImage(withCapInsets:
UIEdgeInsetsMake(h, w, h, w),
resizingMode: .stretch).withRenderingMode(.alwaysTemplate)
cell.bubbleImageView.tintColor = typeColor.withAlphaComponent(0.85)
}
return cell
}
And using a global timer every 10 seconds the app downloads the datas from the net into the globalMessages array, and then reloads the table with notification push: 然后,应用程序每10秒使用一个全局计时器,将数据从网上下载到globalMessages数组中,然后通过通知推送将表重新加载:
@objc func notification_reloadTableView(){
DispatchQueue.main.async(execute: {() -> Void in
let range = NSMakeRange(0, self.tableView.numberOfSections)
let sections = NSIndexSet(indexesIn: range)
self.tableView.reloadSections(sections as IndexSet, with: UITableViewRowAnimation.fade)
})
}
You can insert it like this 您可以这样插入
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row:1, section: 0)], with: .automatic)
tableView.endUpdates()
but first append the new data in the array at the correct position 但首先将新数据附加到数组中的正确位置
globalMessages.insert(["uid": 10, "msg": "NEW"], at: 1)
You can use a dictionary as a lookup table of sorts: 您可以将字典用作各种查找表:
// For simplicity, I'm pretending you have a "Message" object that
// would be in each element of that array you are using.
// This would be a private class level variable.
var messageLookup: [String : Message] = [:]
// And then later, probably in a method you can do the following:
var newMessages: [Message] = []
var updatedMessageLookup: [String : Message] = [:]
// You just got your new batch of messages, I'll say they're in
// an array called "messages".
for message in messages
{
if messageLookup[message.UID] == nil
{
// It's not in your lookup, so it is new.
newMessages.append(message)
}
// As we enumerate, build up what will become the new lookup.
updatedMessageLookup[message.UID] = message
}
messageLookup = updatedMessageLookup
At the end, the newMessages array will have your, well, new messages. 最后,newMessages数组将包含您的新消息。
let's say you have two arrays, newArray
and oldArray
. 假设您有两个数组
newArray
和oldArray
。 You iterate the newArray
and check if the item existing in the oldArray
, for those are not, make the indexes as indexPaths, and put them in an array called insertIndexPaths
. 您迭代
newArray
并检查oldArray
是否存在该项目,如果不存在,则将索引设置为indexPaths,然后将它们放入一个名为insertIndexPaths
的数组中。 The do the same thing to iterate the oldArray
and find out the items not existing and put them in removeIndexPaths
. 可以执行相同的操作来迭代
oldArray
并找出不存在的项目并将其放入removeIndexPaths
。 And finally you call insertRowsAtIndexPaths
or deleteRowsAtIndexPaths
from the tableView
最后,您从
tableView
调用insertRowsAtIndexPaths
或deleteRowsAtIndexPaths
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.