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

时间:2013-06-26 06:29:56

标签: ios objective-c uitableview

我已经使用自定义单元格实现了tableview。在单元格上有图像视图,我添加了手势识别器。但是当我点击imageview来触发一些事件时,它会转到带有数字的方法,因为我已经滚动了表格视图。假设我滚动了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;

4 个答案:

答案 0 :(得分:3)

这是因为您在cell == nil条件之外添加了手势识别器。 每次滚动时,TableView都会调用cellForRowAtIndexPath:委托方法。因此,您可以通过在cell == nil条件中添加手势识别器来避免这种情况。

答案 1 :(得分:1)

试试这个:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CustomTableViewCell";
    CustomTableViewCell *cell = (CustomTableViewCell *)[tblNameList dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)  {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.showsReorderControl = NO;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor clearColor];

        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 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];

    [cell.image1 setImageWithURL:[NSURL URLWithString:filePath1]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    [cell.image2 setImageWithURL:[NSURL URLWithString:filePath2]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    return cell;
}

答案 2 :(得分:1)

//试试这个..

//在.h

UITapGestureRecognizer *tap1[100];
UITapGestureRecognizer *tap2[100];

//在.m

{
    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];

    if(!tap1[indexPath.row])
{
    tap1[indexPath.row] = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlefirstTap:)]autorelease];
    tap1.delegate=self;
    tap1.numberOfTapsRequired=1;
    [cell.image1 addGestureRecognizer:tap1];
}
    if(!tap2[indexPath.row])
{
    tap2[indexPath.row] =
    [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesecondTap:)]autorelease];
    tap2.delegate=self;
    tap2.numberOfTapsRequired=1;
    [cell.image2 addGestureRecognizer:tap2];
}
    [cell.image1 setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"image"]]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    [cell.image2 setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"image"]]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

}
return cell;

答案 3 :(得分:1)

//试试这个......根据内存管理,这段代码对你有帮助。

//在自定义单元格中..

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        image1 = [[UIImageView alloc]init];
        [self addSubview: image1];

        tap1 = [[UITapGestureRecognizer alloc] init];
        tap1.numberOfTapsRequired=1;
        [image1 addGestureRecognizer:tap1];
    }
}

//在.m

{
    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];

    [cell.image1 setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[aroundmearray objectAtIndex:indexPath.row*2]objectForKey:@"image"]]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    [cell.image2 setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[aroundmearray objectAtIndex:indexPath.row*2+1]objectForKey:@"image"]]]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

        [cell.tap1 addTarget:self action:@selector(handlefirstTap:)];
        [cell.tap2 addTarget:self action:@selector(handlesecondTap:)];
}
return cell;