0

テーブルビュー cell のカスタム セルを作成しています。customcell クラスに条件を記述しています。indexpath.row=0 の場合、セルに 2 つの画像を作成し、そうでない場合は 3 つの画像を作成します

私のカスタムセルクラス

if (appDelegate.rowIndex==0)
        {
            UIButton *lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
            lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
            [lObjImageButton setBackgroundColor:[UIColor greenColor]];
            //lObjImageButton.imageView.contentMode  = UIViewContentModeScaleAspectFit;
            [self addSubview:lObjImageButton];

            UIButton *lObjImageButton1=[UIButton buttonWithType:UIButtonTypeCustom];
            lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
            [lObjImageButton1 setBackgroundColor:[UIColor redColor]];

            //lObjImageButton1.imageView.contentMode  = UIViewContentModeScaleAspectFit;
            [self addSubview:lObjImageButton1];

            imageButtonArray = [[NSArray alloc] initWithObjects:lObjImageButton, lObjImageButton1, nil];
        }
        else
        {
        UIButton *lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
        lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
        [lObjImageButton setBackgroundColor:[UIColor greenColor]];
        //lObjImageButton.imageView.contentMode  = UIViewContentModeScaleAspectFit;
        [self addSubview:lObjImageButton];

        UIButton *lObjImageButton1=[UIButton buttonWithType:UIButtonTypeCustom];
        lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
        [lObjImageButton1 setBackgroundColor:[UIColor redColor]];

        //lObjImageButton1.imageView.contentMode  = UIViewContentModeScaleAspectFit;
        [self addSubview:lObjImageButton1];


            UIButton *lObjImageButton2=[UIButton buttonWithType:UIButtonTypeCustom];
            lObjImageButton2.frame=CGRectMake(150*2+10+5, 5, 150-10, 170+10);
            [lObjImageButton2 setBackgroundColor:[UIColor blueColor]];

            //lObjImageButton1.imageView.contentMode  = UIViewContentModeScaleAspectFit;
            [self addSubview:lObjImageButton2];

        imageButtonArray = [[NSArray alloc] initWithObjects:lObjImageButton, lObjImageButton1,lObjImageButton2, nil];
        }

ここで appdelegate.rowindex の値は、viewcontroller の内部の cellforrowindex で appdelegate.rowstauts=indexpath.row として設定しています。

アプリケーションを実行すると、プロジェクトは私の希望どおりに実行されます.1番目のセル2つの画像と残りのセル3つの画像ですが、スクロールすると条件が一致せず、6番目のセルが2つの画像になり、合計3つのセルが2つの画像になり、私の最初のセルは 3 つの画像になります..

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

    static NSString *CellIdentifier = @"Cell";

    BSImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    appDelegate.rowIndex=indexPath.row;

    NSLog(@"cell.index= %i",cell.index);
    if (cell == nil) {

        cell = [[BSImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }


    return cell;
}

問題はスクロールで発生します。

4

4 に答える 4

0

- (void)resetCellセルを初期状態にリセットするメソッドを作成- (void)configureImageView:(int)rowIndexし、画像ビューを構成するメソッドも作成しますBSImageCell

- (void)configureImageView:(int)rowIndex
{
    if (rowIndex==0)
    {
        UIButton *lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
        lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
        [lObjImageButton setBackgroundColor:[UIColor greenColor]];
        //lObjImageButton.imageView.contentMode  = UIViewContentModeScaleAspectFit;
        [self addSubview:lObjImageButton];

        UIButton *lObjImageButton1=[UIButton buttonWithType:UIButtonTypeCustom];
        lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
        [lObjImageButton1 setBackgroundColor:[UIColor redColor]];

        //lObjImageButton1.imageView.contentMode  = UIViewContentModeScaleAspectFit;
        [self addSubview:lObjImageButton1];

        imageButtonArray = [[NSArray alloc] initWithObjects:lObjImageButton, lObjImageButton1, nil];
    }
    else
    {
        UIButton *lObjImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
        lObjImageButton.frame=CGRectMake(150*0+12.5, 5, 150-10, 290 - 120+10);
        [lObjImageButton setBackgroundColor:[UIColor greenColor]];
        //lObjImageButton.imageView.contentMode  = UIViewContentModeScaleAspectFit;
        [self addSubview:lObjImageButton];

        UIButton *lObjImageButton1=[UIButton buttonWithType:UIButtonTypeCustom];
        lObjImageButton1.frame=CGRectMake(150*1+10+5, 5, 150-10, 170+10);
        [lObjImageButton1 setBackgroundColor:[UIColor redColor]];

        //lObjImageButton1.imageView.contentMode  = UIViewContentModeScaleAspectFit;
        [self addSubview:lObjImageButton1];


        UIButton *lObjImageButton2=[UIButton buttonWithType:UIButtonTypeCustom];
        lObjImageButton2.frame=CGRectMake(150*2+10+5, 5, 150-10, 170+10);
        [lObjImageButton2 setBackgroundColor:[UIColor blueColor]];

        //lObjImageButton1.imageView.contentMode  = UIViewContentModeScaleAspectFit;
        [self addSubview:lObjImageButton2];

        imageButtonArray = [[NSArray alloc] initWithObjects:lObjImageButton, lObjImageButton1,lObjImageButton2, nil];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    BSImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
    if (cell == nil) {

        cell = [[BSImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }
    else
    {
        [cell resetCell];
    }
    [cell configureImageView:indexPath];

    return cell;
}
于 2013-08-26T13:30:35.927 に答える
0

カスタム セルの条件をチェックする代わりに、ポリモーフィズムを使用します。

2 つのクラスを記述します。

@interface KSRTwoButtonCell : UITableViewCell { ... }

@interface KSRThreeButtonCell : UITableViewCell { ... }

そして、する

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL wantThreeButtonCell = (indexPath.row == 0);

    NSString *cellIdentifier;
    if (wantThreeButtonCell) {
        cellIdentifier = @"ThreeButtonCell";
    }
    else {
        cellIdentifier = @"TwoButtonCell";
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        if (wantThreeButtonCell) {
            cell = [[KSRThreeButtonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        else {
            cell = [[KSRTwoButtonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
    }

    return cell;
}
于 2013-08-26T11:37:42.653 に答える
0

問題は、パフォーマンスのために UITableView がこれらのセルを再利用可能にすることです。

cellForRowAtIndexPath でもこの条件を設定する必要があります。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* reusableCellIdentifier = @"";
  if(indexPath.row == 0){
    reusableCellIdentifier = @"firstCell";
  }else{
    reusableCellIdentifier = @"otherCells";
  }
}
于 2013-08-26T11:17:29.893 に答える