2017-02-13 7 views
1

私はUITableViewCellでそれぞれのボタンをクリックしてサーバーからファイルをダウンロードする必要があります。ユーザーが1つのセルのダウンロードでダウンロードするボタンを開始します。ダウンロードの完了後、コアデータを保存します。これまですべてがうまくいった。しかし、現在のファイルをダウンロードしている間に、ユーザーがCellの別のファイルをダウンロードするようにタップすると、ダウンロードしてコアデータに保存し、再生できるようにする必要があります。ボタンはそれらをダウンロードし、コアデータに保存する必要があります。ここで私のコード。ここでサーバーからファイルを1つずつダウンロードします

  NSString *url=[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"voice"]; 
     NSLog(@"%@",url); 
     //NSURL *voiceUrl=[NSURL URLWithString:url]; 

     tempDict=[[NSMutableDictionary alloc]init]; 

     [tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"msg_id"] forKey:@"msg_id"]; 
     [tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"to"] forKey:@"to"]; 
     [tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"from"] forKey:@"from"]; 
     [tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"time"] forKey:@"time"]; 

     UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:cell.playButton.bounds]; 
     animatedImageView.animationImages = [NSArray arrayWithObjects: 
              [UIImage imageNamed:@"1.png"], 
              [UIImage imageNamed:@"2.png"], 
              [UIImage imageNamed:@"3.png"], 
              [UIImage imageNamed:@"4.png"], nil]; 
     animatedImageView.animationDuration = 3.0f; 
     animatedImageView.animationRepeatCount = 10; 
     [animatedImageView startAnimating]; 
     [cell1.playButton addSubview: animatedImageView]; 

     NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
     AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 
     [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]]; 
     // manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/octet-stream",@"video/3gpp",@"audio/mp4",nil]; 
     NSURL *URL = [NSURL URLWithString:url]; 
     NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

     NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { 
      if (error) 
      { 
       NSLog(@"Error: %@", error); 
      } else 
      { 

       NSData *data=[[NSData alloc]initWithData:responseObject]; 

      //Here I'm saving file to local storage then updating UI. 
        [self sentMsgSaveWithData:data orUrl:@"" withBool:YES withMsg_ID:@"" withDict:tempDict]; 

      } 
     }]; 
     [dataTask resume]; 

私は、ユーザーが別のセルをタップした場合のみ、私はセル内の複数のボタンのタップで複数のファイルをダウンロードする必要がit.Butをダウンロードしてる時に一つだけのファイルをダウンロードするために管理し、その完了後。 私はこれを実装するために多くの努力を払っています。いくつかの提案をお願いします。
ありがとうございます。

+0

が重複する可能性[ファイルをダウンロードしてAFNetworking 3.0を使用してローカルに保存する方法?](http://stackoverflow.com/questions/35137749/how-to-download-files-and-save-to-local-usin g-afnetworking-3-0) –

+0

あなたはopenurl(NSUrl *)mypicUrl:(Int)cellrowと言う方法でURLを渡します。したがって、どのセルを再ロードする必要があるのか​​を知ることができます。 – user3344236

答えて

0

MVCパターンでは、セルはビューであり、データの解析とダウンロードを処理すべきではありません。あなたのモデルでそれを行う方が良いです。しかしシンプルさのために、しばしばコントローラに入れられます。

  • (...デリゲートまたはブロックまたは通知)をコントローラでモデルの配列を保持し、
  • がコントローラにあなたのセルのボタンアクションを設定するセルにデータを渡す

    1. は、あなたのコントローラにダウンロードするコードを入れて、あなたのモデルを更新します完了後にステータスとリロードtableView。

    cell.h

    #import <UIKit/UIKit.h> 
    #import "YourCellProtocol.h" 
    
    typedef NS_ENUM(NSInteger, YourCellStatus) { 
        YourCellStatusNormal, 
        YourCellStatusDownloading, 
        YourCellStatusCompleted 
    }; 
    
    @interface YourCell : UITableViewCell 
    
    @property (nonatomic, weak) id<YourCellProtocol> delegate; 
    
    @property (nonatomic, assign) YourCellStatus status; 
    
    @property (nonatomic, weak) id yourDataUsedToShownInUI; 
    
    @end 
    

    cell.m

    #import "YourCell.h" 
    
    @interface YourCell() 
    
    @property (nonatomic, strong) UIButton *myButton; 
    
    @end 
    
    @implementation YourCell 
    
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 
         // init all your buttons etc 
         _myButton = [UIButton buttonWithType:UIButtonTypeSystem]; 
         [_myButton addTarget:self action:@selector(myButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 
         [self.contentView addSubview:_myButton]; 
        } 
        return self; 
    } 
    
    - (void)setStatus:(YourCellStatus)status { 
        //update your cell UI here 
    } 
    
    - (void)myButtonPressed { 
        // tell your controller to start downloading 
        if (self.status != YourCellStatusNormal) { 
         [self.delegate didPressedButtonInYourCell:self]; 
        } 
    } 
    
    @end 
    

    controller.m

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
        static NSString *CellID = @"YourCellID"; 
        YourCell *cell = (YourCell *)[tableView dequeueReusableCellWithIdentifier:CellID]; 
    
        if (cell == nil) { 
         cell = [[YourCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID]; 
        } 
    
        cell.delegate = self; 
    
        YourModel *model = self.yourDataArray[indexPath.row]; 
        cell.yourDataUsedToShownInUI = model.dataToShownInUI; 
        if (model.downloading) { 
         cell.status = YourCellStatusDownloading; 
        } else if (model.completed) { 
         cell.status = YourCellStatusCompleted; 
        } else { 
         cell.status = YourCellStatusNormal; 
        } 
    
        //other configs ... 
    
        return cell; 
    } 
    
    - (void)didPressedButtonInYourCell:(id)sender { 
        NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; 
        YourModel *model = self.yourDataArray[indexPath.row]; 
        model.downloading = YES; 
    
        //start downloading 
        //... 
        // in download completion handler, update your model status, and call 
        //[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 
    
  • 関連する問題