问题
I'm trying to develop a progress view inside UITableViewCell
.
In my first realization I used AFHTTPRequestOperation
and its setDownloadProgressBlock:^
inside my cell.
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 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).
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?
回答1:
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];
in the progress call back block of AFHTTPRequestOperation
, send out a name:kSongDownloadProgressUpdateNotificaton
with a song id identifier.
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.
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.
来源:https://stackoverflow.com/questions/14658319/download-progress-in-cell