[英]Download option on long press of UITableViewCell
我有一個用數組填充的UITableView,它包含單元格標簽和單元格詳細信息文本,詳細信息文本基本上是保存文件的URL。 我希望在長按uitableviewcell時彈出一個彈出窗口,該彈出窗口可以選擇下載文件,然后單擊該選項將文件保存在手機內存中的特定目錄中。
我怎樣才能做到這一點?
向您的單元格對象添加手勢
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];
}
長按UITableViewCell
可以使用UILongPressGestureRecognizer 。 要顯示選項,可以使用UIActionSheet 。 要下載文件,可以使用NSURLConnection或ASIHTTPRequest 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.