ios - 在 tableview 滚动期间将事件添加到 imageview

标签 ios objective-c uitableview

我已经实现了带有自定义单元格的表格 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/

相关文章:

ios - performSegue 动画完成,什么标识符?

ios - 将一个 NSAttributedString 的属性应用到另一个

ios - 为什么 UITextField 在 vi​​ewDidAppear 中调用 becomeFirstResponder 会减慢当前动画的速度?

objective-c - `[NSScreen mainscreen]` 误导文档

ios - ScrollView 动画不流畅(objective-C)

ios - 通过 SdWebImage 设置图像给出 “Error code=-1003” 并使用 CFNetwork 诊断日志 "Err(-65554)"

ios - 强制用户在导航之前在 TableView 中选择一行

ios - 如何滚动到 UITableView 的确切末尾?

javascript - 在 keyup 函数运行时保持 iOS 键盘打开

objective-c - 何时使用 enumerateObjectsUsingBlock 与 for