长按UITableViewCell下载选项

时间:2013-04-24 05:12:09

标签: ios xcode uitableview downloadfile

我有一个用数组填充的UITableView,它包含单元格标签和单元格详细信息文本,详细文本基本上是保存文件的URL。我希望在长按uitableviewcell时弹出一个弹出窗口,可以选择下载文件,点击该选项后文件将保存在手机内存的特定目录中。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

向您的手机对象添加手势

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

处理它

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


}

下载文件的响应方法

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

答案 1 :(得分:0)

如果长按UITableViewCell,您可以使用UILongPressGestureRecognizer。 要显示选项,您可以使用UIActionSheet。 要下载文件,您可以使用NSURLConnectionASIHTTPRequest