简体   繁体   中英

Download option on long press of UITableViewCell

I have a UITableView that is populated with an array, it contains cell label and cell detail text, the detail text is basically the URL at which the file is saved. I want that on long press of the uitableviewcell a pop up comes up which has the option to download the file and on clicking that option the file is saved in a specific directory on the phone memory.

How can i do that?

Add gesture to your cell object

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
  initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0; //seconds
lpgr.delegate = self;
[objMyTableViewCell addGestureRecognizer:lpgr];
[lpgr release];

Handle it

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
   UITableVIewCell *objTableCell = (UITableVIewCell*)gestureRecognizer
   NSURL *url = [NSURL URLWithString:objTableCell.lable.text];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestDone:)];
    [request setDidFailSelector:@selector(requestWentWrong:)];
    [request setDownloadDestinationPath:[NSString stringWithFormat:@"%@",filePath]]; //use the path from earlier
    [queue addOperation:request]; //queue is an NSOperationQueue
    [request setDownloadProgressDelegate:self];
    [request setShowAccurateProgress:YES];


}

Response method for download file

- (void)requestDone:(ASIHTTPRequest *)request
{
    NSString *response = [request responseString];
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle: @"hurreh!!"
                          message: @"Your download complete"
                          delegate: nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    //Do something useful with the content of that request.
}



- (void)requestWentWrong:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
}

For long press on the UITableViewCell you can use the UILongPressGestureRecognizer . To show the options you can use the UIActionSheet . To download file you can use NSURLConnection or ASIHTTPRequest .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM