Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  2.  
  3. static NSString *CellIdentifier = @"Cell";
  4. cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  5. if (cell == nil)
  6. {
  7. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  8. }
  9. // Download Button
  10. DownloadButtonCell=[[UIButton alloc]initWithFrame:CGRectMake(10, 30, 48, 48)];
  11. [DownloadButtonCell setImage:[UIImage imageNamed:@"download.png"] forState:UIControlStateNormal];
  12. DownloadButtonCell.tag=111;
  13. [DownloadButtonCell addTarget:self action:@selector(startDownload:)
  14. forControlEvents:UIControlEventTouchUpInside];
  15. [cell.contentView addSubview:DownloadButtonCell];
  16. return cell;
  17. }
  18.  
  19. -(void)startDownload:(id)sender
  20. {
  21. CGPoint buttonPosition=[sender convertPoint:CGPointZero toView:self.tableview];
  22. NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:buttonPosition];
  23. NSLog(@"%ld",(long)indexPath.row);
  24.  
  25. LLACircularProgressView *progressView=[[LLACircularProgressView alloc]initWithFrame:CGRectMake(20, 40, 25, 25)];
  26. progressView.tintColor = [UIColor greenColor];
  27. [progressView addTarget:self action:@selector(stopDownload:) forControlEvents:UIControlEventTouchUpInside];
  28.  
  29. [[[tableview cellForRowAtIndexPath:indexPath] viewWithTag:111]removeFromSuperview];
  30. [[[tableview cellForRowAtIndexPath:indexPath]contentView] addSubview:progressView];
  31.  
  32. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[mp3Array objectAtIndex:indexPath.row]]];
  33. operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  34. NSString *pdfName = [[idArray objectAtIndex:indexPath.row] stringByAppendingString:@".mp3"];
  35.  
  36. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  37. NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pdfName];
  38. operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
  39.  
  40. [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  41. NSLog(@"Successfully downloaded file to %@", path);
  42. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  43. NSLog(@"Error: %@", error);
  44. }];
  45. [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
  46.  
  47. progressView.progress=(float)totalBytesRead / totalBytesExpectedToRead;
  48. }];
  49. [operation start];
  50. }
  51.  
  52. //DownloadableTableViewCell.h
  53. @interface DownloadableTableViewCell : UITableViewCell
  54. @property (assign, nonatomic, getter=isDownloading) BOOL downloading;
  55. @end
  56.  
  57. //DownloadabletableViewCell.m
  58. @interface DownloadableTableViewCell()
  59. @property (strong, nonatomic) UIView *downloadButton;
  60. @property (strong, nonatomic) UIView *progressView;
  61. @end
  62.  
  63. @implementation
  64. -(id)initWithStyeleBlahblah{
  65. self = [super initWithStyleBlahBlah];
  66. if (self){
  67. //init download button, init progress view.
  68.  
  69. //add download button to view.
  70. }
  71. return self;
  72. }
  73.  
  74. -(void)setDownloading:(BOOL)downloading{
  75. _downloading = downloading;
  76. if (_downloading){
  77. //add progress view to contentview.
  78. //remove download button.
  79. }
  80. else{
  81. //add button to contentView;
  82. //remove progress view;
  83. }
  84. }
  85.  
  86.  
  87. -(void)prepareForReuse{
  88. [super prepareForReuse];
  89. self.downloading = NO;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement