2

我已经用自定义单元格实现了表格视图。单元格上有图像视图,我在其中添加了手势识别器。但是,当我单击 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

4 回答 4

3

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

于 2013-06-26T06:38:54.210 回答
1

// Try this..

//in .h

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

//in .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;
于 2013-06-26T07:08:58.023 回答
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;
}
于 2013-06-26T06:37:46.463 回答
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;
于 2013-06-26T07:34:38.670 回答