我正在尝试在imageView
内的tableView cell
上添加点按手势。
问题是如果我将手势代码放在cellForRow
中,它无法识别网址,当然所有图像都会获得最后一个单元格的网址。
如果我将手势代码放在didSelect
中,则url总是为空,我认为因为手势在单元格获取数据之前正在工作。
imageView应根据其网址打开一个视频文件,该文件从XML解析器中获取。
selectedArticle = [self getArticleAtIndex:indexPath];
UIImageView* imageTap = [
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(actionHandleTapOnImageView)];
[singleTap setNumberOfTapsRequired:1];
imageTap.userInteractionEnabled = YES;
[imageTap addGestureRecognizer:singleTap]
(void)actionHandleTapOnImageView{
NSString *path = selectedArticle.videoUrl;
NSURL *videoURL = [NSURL URLWithString:path];
MPMoviePlayerViewController *theArticle = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:theArticle];
theArticle.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[theArticle.moviePlayer play];
}
答案 0 :(得分:4)
我认为最好添加一个按钮而不是一个imageview。代码应该看起来像这样:
// in cell for row:
UIButton *buttonImage = [UIButton buttonWithType:UIButtonTypeCustom];
buttonImage.frame = CGRectMake(5.0, 5.0, 40.0, 40.0);
buttonImage.tag = indexPath.row;
[buttonImage setBackgroundImage:yourImage forState:UIControlStateNormal];
[buttonImage addTarget:self action:@selector(imageTap:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:buttonImage];
然后在imageTap
:
- (void)imageTap:(UIButton *)sender
{
selectedArticle = [self getArticleAtIndex:sender.tag];
NSString *path = selectedArticle.videoUrl;
NSURL *videoURL = [NSURL URLWithString:path];
MPMoviePlayerViewController *theArticle = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:theArticle];
theArticle.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[theArticle.moviePlayer play];
}
此方法可以防止您在表格单元格中使用点击识别器的某些副作用。它很容易理解和纠正。