5

カスタムセルを作成していますUILabel, ,動的タグを使用して、UIImageView定数タグを使用して、テーブルには11個のセルがあり、最初の7個のセルが正しくロードされ、1, 2 、3、4、それぞれテーブルのセル、タグもセル内で同じです。画像を使用してテーブルのチェックボックスをオンにし、テーブルのイメージビューを変更するために使用します。UILabelUIImageViewUITapGestureRecognizer

私はこのコードを使用しています.....

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier =@"Cell";
    UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
        cell.selectionStyle=UITableViewCellSelectionStyleGray;
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(5, 12, 20, 20)];
        imageview.tag=n;
        [cell.contentView addSubview:imageview]; 
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tabimage:)];
        imageview.userInteractionEnabled=YES;
        [imageview addGestureRecognizer:tap];
        imageview.image=[UIImage imageNamed:@"img1.jpeg"];

        UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 2, 260,26)];
        titleLabel.tag=222;
        titleLabel.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:titleLabel];

        UILabel *dateLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 31, 260, 13)];
        dateLabel.tag=333;
        dateLabel.font=[UIFont systemFontOfSize:10];
        dateLabel.backgroundColor=[UIColor clearColor];
         [cell.contentView addSubview:dateLabel];
    }
    UIImageView *imageview1=(UIImageView*)[cell.contentView viewWithTag:n];
    if([array containsObject:[NSNumber numberWithInt:imageview1.tag]]) {
       imageview1.image=[UIImage imageNamed:@"img2.jpeg"];  
    } else {
       imageview1.image=[UIImage imageNamed:@"img1.jpeg"];   
    }

    UILabel *titlelable=(UILabel*)[cell.contentView viewWithTag:222];
    titlelable.text=[task objectAtIndex:indexPath.section];
    NSLog(@"%i",indexPath.section);

    UILabel *dateLabel=(UILabel*)[cell.contentView viewWithTag:333];
    dateLabel.text=[date objectAtIndex:indexPath.section];
    n++;
    return  cell;
}

- (void)tabimage:(id)sender {
    UIImageView *iv=(UIImageView *)[sender view];
    int i=iv.tag;
    NSLog(@"------------%i",i);
   if (iv.image==[UIImage imageNamed:@"img1.jpeg"]) {
       iv.image= [UIImage imageNamed:@"img2.jpeg"];
       [array addObject:[NSNumber numberWithInt:i]];
   } else {
      iv.image= [UIImage imageNamed:@"img1.jpeg"];  
      [array removeObject:[NSNumber numberWithInt:i]];
    }
  }
4

5 に答える 5

8

問題を解決するには、セル識別子を静的から動的に変更する必要があります。

これを交換する必要があります

static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier];

これとともに

UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%d %d"], indexPath.row, indexPath.section];
于 2012-10-03T15:05:59.120 に答える
4

どのような問題が発生したとしても、その「n」の使用がおそらく問題です。共有変数を使用してテーブルビューセルのビューにタグを付ける(および取得する)理由はありません。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathは何度も呼び出され、同じセルに対して何度も呼び出される場合があり、再利用するセルが見つかる場合と見つからない場合があります。

もちろん、それが何でnあるか、コードの他の場所でどのように使用するかはわかりませんが、ここで呼び出すたびに変更し、このメソッドが呼び出されたときに仮定を立てることができないため、価値がありません。

すべてのセルにそのタグが付いた画像が1つあることは確かですが、このメソッドを最初に実行した後にアクセスできるかどうかは完全に定義されていないため、再利用するたびに基本的にランダムな動作が発生します(新しいセルまたは知らないタグ​​が付いた再利用されたもの)。

自問してみてください(または別の質問でSOで):ここで何を達成しようとしていますか?

于 2012-10-03T14:06:45.067 に答える
2

アプリのソリューションを取得しています。

コードで動的セル識別子を使用するには

NSString *CellIdentifier =[NSString stringWithFormat:@"%i-%i", indexPath.section,indexPath.row];
于 2012-10-26T11:24:53.583 に答える
1

UITableViewCell をサブクラス化して、そこにすべてのカスタム要素を追加し、セッターを作成することをお勧めします。cellForRowAtIndexPath では、セッターを呼び出すだけです。

于 2012-10-25T06:23:46.803 に答える
0

if(cell == nil) ブロックでタグを imageview に設定しているため、セルが作成されるときにタグが設定されることを意味します。あなたの場合、デキューによってセルが再利用された後、7つのセルが作成されます。したがって、8 以降は以前に作成されたセルが使用されます。これが、8,9,10... セルのタグが 1,2,3... と同じである理由です。

さて、アレックスはそれを行う標準的な方法を提案しました。ただし、カスタムセルをまだ使用していない場合は、これを試すことができます-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier =@"Cell";
    UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier];
    UIImageView *imageview = nil;    
    if(cell){
        for (UIView *view in cell.contentView.subviews){
            if([view isKindOfClass:[UIImageView class]]){
                    imageview = (UIImageView*)view;
                    break;
            }
        }
    }

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
        cell.selectionStyle=UITableViewCellSelectionStyleGray;
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        imageview=[[UIImageView alloc]initWithFrame:CGRectMake(5, 12, 20, 20)];
        [cell.contentView addSubview:imageview]; 
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tabimage:)];
        imageview.userInteractionEnabled=YES;
        [imageview addGestureRecognizer:tap];
        imageview.image=[UIImage imageNamed:@"img1.jpeg"];

        UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 2, 260,26)];
        titleLabel.tag=222;
        titleLabel.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:titleLabel];

        UILabel *dateLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 31, 260, 13)];
        dateLabel.tag=333;
        dateLabel.font=[UIFont systemFontOfSize:10];
        dateLabel.backgroundColor=[UIColor clearColor];
         [cell.contentView addSubview:dateLabel];
    }
    imageview.tag=n;
    if([array containsObject:[NSNumber numberWithInt:imageview.tag]]) {
       imageview.image=[UIImage imageNamed:@"img2.jpeg"];  
    } else {
       imageview.image=[UIImage imageNamed:@"img1.jpeg"];   
    }

    UILabel *titlelable=(UILabel*)[cell.contentView viewWithTag:222];
    titlelable.text=[task objectAtIndex:indexPath.section];
    NSLog(@"%i",indexPath.section);

    UILabel *dateLabel=(UILabel*)[cell.contentView viewWithTag:333];
    dateLabel.text=[date objectAtIndex:indexPath.section];
    n++;
    return  cell;
}
于 2012-10-26T06:24:14.283 に答える