我正在使用UITableViewCell
风格的UITableViewCellStyleSubtitle
对象(即左侧的图像视图,粗体的文本标签和详细文本标签下)来创建我的表格。现在我需要检测UIImageView
上的触摸,还要知道单击图像视图的索引路径/单元格。我试过用
cell.textLabel.text = @"Sometext";
NSString *path = [[NSBundle mainBundle] pathForResource:@"emptystar1" ofType:@"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;
cell.imageView.userInteractionEnabled = YES;
但它不起作用。每当点击图像时,都会调用didSelectRowAtIndexPath:
。我不想创建一个单独的UITableViewCell
并添加一个自定义按钮。有没有办法检测UIImageView
本身的触摸?
在你的cellForRowAtIndexPath
方法中添加此代码
cell.imageView.userInteractionEnabled = YES;
cell.imageView.tag = indexPath.row;
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
[tapped release];
然后检查点击了哪个imageView
,检查selector
方法中的标志
-(void)myFunction :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(@"Tag = %d", gesture.view.tag);
}
目前已接受的解决方案在iOS 5.0中被破坏。该错误导致图像视图的手势识别器永远不会被触发。通过对官方开发人员论坛的研究,我发现这是iOS 5.0中的已知错误。它是由内部实现导致-gestureRecognizerShouldBegin:
返回NO。当您将手势识别器的委托设置为自定义UITableViewCell
子类本身时,会出现该错误。
修复是在手势识别器的委托中覆盖-gestureRecognizerShouldBegin:
并返回YES。应该在iOS 5.x的未来版本中修复此错误。只要您没有使用新的UITableViewCell复制/粘贴API,这只是安全的。
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
对于Swift,在您的cellForRowAtIndexPath方法中添加此代码
cell.imageView?.userInteractionEnabled = true
cell.imageView?.tag = indexPath.row
var tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped.numberOfTapsRequired = 1
cell.imageView?.addGestureRecognizer(tapped)
然后检查单击了哪个imageView,检查选择器方法中的标志
func TappedOnImage(sender:UITapGestureRecognizer){
println(sender.view?.tag)
}
你可以做的一种方法是从你的图像创建一个UIImageView并添加一个手势识别器。请参阅下面的示例
//Create ImageView
UIImageView *theImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"emptystar1.png"]];
theImageView.userInteractionEnabled = YES;
//Add Gesture Recognizer
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageSelectedInTable)];
tapped.numberOfTapsRequired = 1;
[theImageView addGestureRecognizer:tapped];
[cell addSubview:theImageView];
//Memory Cleanup
[tapped release];
[theImageView release];
-(void)imageSelectedInTable
{
NSLog(@"Selected an Image");
}
但是,你现在必须更多地布置你的单元格,因为你不能简单地使用UIImageView
的UITableViewCell
属性,因为它只是readonly。
只有在我的情况下,我必须回答从NSFetchedResultsController检索到的表格单元的图像文件上的轻击手势的附加信息,所以我们需要indexPath而不是行,所以我决定使用图像轻敲自己而不是行信息。我为图像定义了一个变量:
var imgSelected: UIImageView? = nil
我将手势识别器添加到imageView(imgZona):
public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath) as! ZonasTableViewCell
let zona = fetchedResultsController.objectAtIndexPath(indexPath) as! Zona
cell.infoZona = zona
let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
cell.imgZona?.addGestureRecognizer(tapGestureRecognizer)
cell.imgZona?.userInteractionEnabled = true
return cell
}
在“imageTapped”方法中,我检索了图像并保存到变量“imgSelected”:
func imageTapped(sender: UITapGestureRecognizer) {
imgSelected = sender.view as? UIImageView
self.performSegueWithIdentifier("MuestraImagen", sender: self)
}
最后,在“prepareForSegue”方法中,我发送了viewController专用的图像来显示被点击的图像:
override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
switch identifier {
case "MuestraImagen":
if let vc = segue.destinationViewController as? MuestraImagenViewController {
if imgSelected != nil {
if let img = imgSelected!.image {
vc.imgPuente = img
}
}
}
default: break
}
}
}
这个问题是:
- >以下代码可以正常使用我:
cell.imageView.image = [UIImage imageNamed:@"my_image.png"];
[cell.imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[cell.imageView addGestureRecognizer:singleTap];
和“singleTapGestureCaptured”功能
- (void)singleTapGestureCaptured:(UITapGestureRecognizer*)gesture{
NSLog(@"Left Image clicked");
}