首页 > 代码库 > 聊天界面之进度条cell(一)

聊天界面之进度条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(一)