Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- }
- // Download Button
- DownloadButtonCell=[[UIButton alloc]initWithFrame:CGRectMake(10, 30, 48, 48)];
- [DownloadButtonCell setImage:[UIImage imageNamed:@"download.png"] forState:UIControlStateNormal];
- DownloadButtonCell.tag=111;
- [DownloadButtonCell addTarget:self action:@selector(startDownload:)
- forControlEvents:UIControlEventTouchUpInside];
- [cell.contentView addSubview:DownloadButtonCell];
- return cell;
- }
- -(void)startDownload:(id)sender
- {
- CGPoint buttonPosition=[sender convertPoint:CGPointZero toView:self.tableview];
- NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:buttonPosition];
- NSLog(@"%ld",(long)indexPath.row);
- LLACircularProgressView *progressView=[[LLACircularProgressView alloc]initWithFrame:CGRectMake(20, 40, 25, 25)];
- progressView.tintColor = [UIColor greenColor];
- [progressView addTarget:self action:@selector(stopDownload:) forControlEvents:UIControlEventTouchUpInside];
- [[[tableview cellForRowAtIndexPath:indexPath] viewWithTag:111]removeFromSuperview];
- [[[tableview cellForRowAtIndexPath:indexPath]contentView] addSubview:progressView];
- NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[mp3Array objectAtIndex:indexPath.row]]];
- operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
- NSString *pdfName = [[idArray objectAtIndex:indexPath.row] stringByAppendingString:@".mp3"];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pdfName];
- operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
- [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSLog(@"Successfully downloaded file to %@", path);
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- NSLog(@"Error: %@", error);
- }];
- [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
- progressView.progress=(float)totalBytesRead / totalBytesExpectedToRead;
- }];
- [operation start];
- }
- //DownloadableTableViewCell.h
- @interface DownloadableTableViewCell : UITableViewCell
- @property (assign, nonatomic, getter=isDownloading) BOOL downloading;
- @end
- //DownloadabletableViewCell.m
- @interface DownloadableTableViewCell()
- @property (strong, nonatomic) UIView *downloadButton;
- @property (strong, nonatomic) UIView *progressView;
- @end
- @implementation
- -(id)initWithStyeleBlahblah{
- self = [super initWithStyleBlahBlah];
- if (self){
- //init download button, init progress view.
- //add download button to view.
- }
- return self;
- }
- -(void)setDownloading:(BOOL)downloading{
- _downloading = downloading;
- if (_downloading){
- //add progress view to contentview.
- //remove download button.
- }
- else{
- //add button to contentView;
- //remove progress view;
- }
- }
- -(void)prepareForReuse{
- [super prepareForReuse];
- self.downloading = NO;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement