我已经实现了带有自定义单元格的表格 View 。单元格上有 ImageView ,我向其添加了手势识别器。但是,当我单击 imageview 以触发某些事件时,它会转到带有数字的方法,因为我已经滚动了表格 View 。假设我滚动了 tableview 5 次,然后 imageview 也触发了该方法 5 次。
这是代码:
{
if (cell == nil)
{
cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.frame = CGRectMake(0.0, 0.0, 320.0, 200.0);
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor redColor];
[cell.image1 setTag:indexPath.row*2];
[cell.image2 setTag:indexPath.row*2+1];
NSString *pathimage1 =[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"image"];
NSString *filePath1 = [NSString stringWithFormat:@"%@",pathimage1];
NSString *pathimage2=[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"image"];
NSString *filePath2 = [NSString stringWithFormat:@"%@",pathimage2];
UITapGestureRecognizer *tap1 =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlefirstTap:)];
tap1.delegate=self;
tap1.numberOfTapsRequired=1;
[cell.image1 addGestureRecognizer:tap1];
[tap1 release];
UITapGestureRecognizer *tap2 =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesecondTap:)];
tap2.delegate=self;
tap2.numberOfTapsRequired=1;
[cell.image2 addGestureRecognizer:tap2];
[tap2 release];
[cell.image1 setImageWithURL:[NSURL URLWithString:filePath1]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
[cell.image2 setImageWithURL:[NSURL URLWithString:filePath2]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
}
return cell;
最佳答案
这是因为您在 cell == nil
条件的外部添加了手势识别器。
TableView
将在每次滚动时调用 cellForRowAtIndexPath:
委托(delegate)方法。因此,您可以通过在 cell == nil
条件中添加手势识别器来避免这种情况。
关于ios - 在 tableview 滚动期间将事件添加到 imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17313010/