2017-02-13 9 views
1

Ich muss Datei vom Server auf entsprechende Schaltfläche klicken in UITableViewCell herunterladen.Wenn Benutzer auf Schaltfläche zum Download in einer Zelle tippen Download sollte gestartet werden.Nach dem Abschluss des Downloads Ich spare in Kerndaten. Bis zu diesem Ganzen ging gut.Aber beim Herunterladen der aktuellen Datei, wenn der Benutzer tippt, um eine andere Datei in Cell herunterzuladen, sollte es auch herunterladen dann zu Kerndaten speichern und zur Verfügung stellen, um zu spielen.Und ich habe verschiedene URLs in jeder Tabelle Zelle.Wenn der Benutzer mehrere klopft Schaltflächen sollten sie herunterladen und in den Kerndaten speichern. Hier mein Code.Download Datei vom Server eins nach dem anderen

  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]; 

Hier konnte ich nur eine Datei zu einem Zeitpunkt und nach Abschluss der, dass zum Download, wenn der Benutzer dann ich it.But bin das Herunterladen nur eine andere Zelle tippt ich mehrere Dateien auf mehreren Tasten Taps in Cell herunterladen muß. Ich habe viel Mühe, dies zu implementieren. Bitte geben Sie einige Vorschläge.
Vielen Dank im Voraus.

+0

Mögliche Duplikat von [Wie Dateien herunterzuladen und zu den lokalen Speichern mit AFNetworking 3.0 ?] (http://stackoverflow.com/questions/35137749/how-to-download-files-and-save-to-local-usin g-afnetworking-3-0) –

+0

Sie werden URL zu einer Methode übergeben, sagen wir Openurl (NSUrl *) mypicUrl: (Int) Zelle. So wissen Sie am Ende des Downloads, welche Zelle neu geladen werden muss. – user3344236

Antwort

0

In MVC-Mustern ist Zelle eine Ansicht und sollte keine Daten analysieren und herunterladen. Es ist besser, es in Ihrem Modell zu tun. Aber für einfach wird es oft in den Controller gesetzt.

  1. Halten Sie Ihr Modell-Arrays in der Steuerung und Daten übergeben Zelle
  2. Ihre Zelle Schaltfläche Aktion so konfigurieren, dass die Steuerung (Delegierter oder Block oder eine Benachrichtigung ...)
  3. Put-Download-Code in Ihrem Controller und Ihr Modell aktualisieren status und reload tableView nach Abschluss.

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]; 
} 
Verwandte Themen