下载单元格中的进度 [英] Download progress in cell

查看:154
本文介绍了下载单元格中的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 UITableViewCell 中开发进度视图。

I'm trying to develop a progress view inside UITableViewCell.

在我的第一次实现中,我使用<我的单元格内有code> AFHTTPRequestOperation 及其 setDownloadProgressBlock:^

In my first realization I used AFHTTPRequestOperation and its setDownloadProgressBlock:^ inside my cell.

后来我的目标变得更加复杂。我有一个显示不同内容(歌曲播放列表)的 UITableView 。几首播放列表中可能会出现相同的歌曲。当我开始在一个播放列表中下载歌曲并切换到另一个(也包含此歌曲)时,我希望看到第一个播放列表中的下载进度。

Later my goal became more complicated. I have one UITableView that displays different content (song playlists). The same song might exits in several playlists. When I start to download the song in one playlist and switch to another (which also contains this song) I want to see the download progress like in the first playlist.

I已经成功地使它发挥作用。在下载过程开始时,我在全局类中创建一个进度视图。要在单元格上显示此进度视图,我只需使用 addSubview removeFromSuperview 。但我不喜欢这种方式,因为我必须在现有进度视图的基础上添加进度视图(因为应用程序设计,每个单元格都有自己的进度视图)。

I have succeeded to make it work. At the beginning of download process I create a progress view in global class. To show this progress view on the cell I just use addSubview removeFromSuperview. But I don't like this way because I have to add the progress view on top of existing progress view (because every cell has its own progress view due to application design).

现在我使用另一种方法。当我显示单元格时,我将 setDownloadProgressBlock:^ 设置为现有的进度视图。我工作正常,但当我打开另一个播放列表时,另一首歌上会显示进度。我打赌这是因为我使用的可重复使用的单元格。

Now I use another approach. When I show the cell I set setDownloadProgressBlock:^ to an existing progress view. I works fine but when I open another playlist the progress is shown on the other song. I bet it is because of reusable cells I use.

我有点厌倦了实现这个东西,任何想法如何让它正常工作?

I'm a bit tired of implementing this thing, any ideas how to make it work properly?

推荐答案

你是对的,由于重复使用单元,使用下载进度回调连接每个单独的单元是不合适的。为了解决这个问题,我将在 Song 对象中保存下载状态,当它设置为 YES 时,开始观察通知。

You are right, due to cell re-use, it's not proper to connect each individual cell with download progress callback. To solve this, I will save download state in your Song object, when it's set to YES, start observing a notification.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(progressDidUpdate:) name:kSongDownloadProgressUpdateNotificaton object:nil];

AFHTTPRequestOperation 的进度回调块中,使用歌曲ID标识符发送名称:kSongDownloadProgressUpdateNotificaton

in the progress call back block of AFHTTPRequestOperation, send out a name:kSongDownloadProgressUpdateNotificaton with a song id identifier.

在单个单元格的 progressDidUpdate:中,确定它正在更新自身的进度,如果不是忽略它,如果是,更新单元格中的progressView。下载完成后,删除观察通知。

in the progressDidUpdate: of individual cell, identify it's updating progress on itself, if not ignore it, if yes, update the progressView in the cell. When the downloading is finished, remove observation for the notification.

此外,您需要在歌曲对象,因为当UITableView调用 cellForRowAtIndexPath Song 将随时分配给另一个单元格,保存并恢复进度将使下载状态持久。

Additionally, you need to save a download percentage data in the Song object since when UITableView calls cellForRowAtIndexPath and Song will be assigned to another cell at any moment, saving and recovering progress will make the download state persistent.

这篇关于下载单元格中的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆