2

カスタムセルでテーブルビューを実装しました。セルには画像ビューがあり、ジェスチャ認識機能を追加しました。しかし、いくつかのイベントを発生させるためにイメージビューをクリックすると、テーブルビューをスクロールしたときに番号付きのそのメソッドに移動します。テーブルビューを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

//これを試してください..このコードは、メモリ管理に従って役立ちます..

// カスタム セル内..

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

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

    [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 に答える
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 に答える