聊天界面之进度条cell

Posted mlj318

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了聊天界面之进度条cell相关的知识,希望对你有一定的参考价值。

ProgressCell用于显示文件传输的进度,困难点在于根据下载进度更新cell的进度条,先后尝试了几种方法:

1.有新的下载进度时,直接调用reloadData()

2.使用reloadRowsAtIndexPaths(),只更新进度条所在的 cell

这两种方法其实都是重新生成cell,重新设置内容,其实是重新绘制了整个cell.然而根据reloadRowsAtIndexPaths的api说明:

Reloading a row causes the table view to ask its data source for a new cell for that row. The table animates that new cell in as it animates the old row out. Call this method if you want to alert the user that the value of a cell is changing. If, however, notifying the user is not important—that is, you just want to change the value that a cell is displaying—you can get the cell for a particular row and set its new value.

正确的方法是

3.我们需要改变cell显示的内容,数据有更新时,获取cell,更新cell的内容即可。使用update而非reload.如果cell不可见,那也不用更新了。

 

使用update很完美,而使用reload方法,有新的进度更新进度条时会发生闪动的现象。

reload方法与update方法的代码:

    func reloadAtIndex(index:Int) {
        dispatch_async(dispatch_get_main_queue()) {
            let indexPath:NSIndexPath = NSIndexPath(forRow: index, inSection: 0)
            self.tableView.beginUpdates()
            self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
            self.tableView.endUpdates()
        }
    }
    //获取cell直接改变内容不会导致cell闪动,reload会重新生成cell导致闪动
    func updateAtIndex(index:Int,msg:BaseMessage) {
        dispatch_async(dispatch_get_main_queue()) {
            let indexPath:NSIndexPath = NSIndexPath(forRow: index, inSection: 0)
            if let cell = (self.tableView.cellForRowAtIndexPath(indexPath) as? ChatTableViewProgressCellA){
                if msg is ProgressMessage{
                    let message = msg as! ProgressMessage
                    cell.time.text = message.time
                    cell.progress.progress = Float(message.transfered)/Float(message.total)
                    cell.content.text = message.text + (message.speed>=0 ? ":\(message.SpeedDescription)" : "")
                }
            }

        }
    }

  

 

 

以上是关于聊天界面之进度条cell的主要内容,如果未能解决你的问题,请参考以下文章

活动到片段方法调用带有进度条的线程

当片段视图加载是异步任务的一部分时,如何在片段加载之前显示进度条?

自定义对话框片段内的进度条 - 如何从 AsyncTask 传递进度?

用户界面框架jQuery EasyUI示例大全之进度栏搜索框及表单演示

在recyclerview片段Reandroid中屏幕旋转后进度条不显示

css Bootstrap 3进度条片段