数组& TableView:仅添加新项目

时间:2018-02-09 17:42:44

标签: ios arrays swift uitableview

我有一个数据库,想制作一个显示消息的应用程序。

在我的快速应用程序中有一个变量,让我们用一些随机值填充

var globalMessages

{
["uid": 1, "msg": "test1"],
["uid": 2, "msg": "test2"],
["uid": 3, "msg": "test3"],
}

我使用urlSession从网上获取这些数据:

{
["uid": 1, "msg": "test1"],
    ["uid": 10, "msg": "NEW"], //new data
["uid": 2, "msg": "test2"],
["uid": 3, "msg": "test3"]
}

然后我不想重新加载所有表格:我只想添加 [" uid":10," msg":"新的"] 在1.和2.用户的消息之间向表格发送新消息,并用温和的动画推送已经存在的两条消息。

有可能吗?

问题是我无法弄清楚,如何检查新获取的数据数组中哪些项目是新的,哪些不在旧的 globalMessages ,如果有的则显示动画。

如果某个项目不再出现在新获取的数据中,则会从tableview中删除动画。 ??

目前我这样做:

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
}

每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)
        })
}

3 个答案:

答案 0 :(得分:0)

您可以像这样插入

 tableView.beginUpdates()
 tableView.insertRows(at: [IndexPath(row:1, section: 0)], with: .automatic)
 tableView.endUpdates()

但首先将数据附加到数组中的正确位置

globalMessages.insert(["uid": 10, "msg": "NEW"], at: 1)

答案 1 :(得分:0)

您可以使用字典作为各种查找表:

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

最后,newMessages数组将包含您的新消息。

答案 2 :(得分:0)

我们假设您有两个阵列newArrayoldArray。您迭代newArray并检查oldArray中存在的项目(如果不存在),将索引作为indexPaths,并将它们放在名为insertIndexPaths的数组中。做同样的事情来迭代oldArray并找出不存在的项目并将它们放在removeIndexPaths中。最后,您从insertRowsAtIndexPaths

致电deleteRowsAtIndexPathstableView
相关问题