【问题标题】:Swift + Pubnub chat app load old message for scrollingSwift + Pubnub 聊天应用程序加载旧消息以进行滚动
【发布时间】:2017-02-21 05:45:59
【问题描述】:

在我的聊天视图控制器的 viewDidLoad 中,我写了self.appDelegate.client?.historyForChannel(currentChannel, start: nil, end: nil, limit: 20, withCompletion:,它检索了最近的 20 条消息。但是,我希望在最近的 20 条消息之前检索较早/旧的 20 条消息,以实现无限滚动功能。我该怎么做?

【问题讨论】:

  • 您能否详细说明问题中的“我需要检索另外 20 条消息”部分!哪 20 条消息??
  • 说消息历史有40条消息,viewDidLoad检索message[0]~[19],我将如何检索[20]~[39]?

标签: ios swift infinite-scroll pubnub jsqmessagesviewcontroller


【解决方案1】:

存储您从 Pubnub 历史记录中收到的第一条消息的时间戳,以接收接下来的 20 条消息:

self.client?.historyForChannel(channel, start: lastStoredTimstamp, end: nil, limit: 20, reverse: false, withCompletion:

我已经测试过了,效果很好。

小说明: 仅使用 start 参数总是返回比提供的时间令牌更早的消息。如果您设置 reverse = true,您将收到比提供的时间令牌更新的消息。

请参阅https://www.pubnub.com/docs/swift/storage-and-history PubNub 历史 API 如何与时间线图配合使用。

【讨论】:

    【解决方案2】:

    可以通过UIScrollViewDelegate 完成(在您的情况下,它在 UITableView 中)

    首先,设置你的UITableView的代理。

    然后,你必须覆盖scrollViewDidScroll(_ scrollView: UIScrollView),这是一个示例代码:

    let currentOffset = scrollView.contentOffset.y
    let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
    
    let deltaOffset = maximumOffset - currentOffset
    if (deltaOffset <= 0)
       // call to self.appDelegate.client?.historyForChannel with the offsets
    )
    

    当您到达列表底部时,if 表达式会触发。

    self.appDelegate.client?.historyForChannel 包含开始和结束偏移量。可以通过在每次调用 historyForChannel 之后添加 +20(在某处声明一个类变量)来计算开始,但它仅在来自响应的对象没有某些唯一 ID 时才有效。

    【讨论】:

    • 问题是startend 是NSNumbers,它们应该是一个日期。这就是为什么我不能只为每个滚动做一个简单的 +20
    猜你喜欢
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多