0

作成したセルを含むテーブル ビューのコードがあります。各セルには 3 つのボタンがあります。各ボタンには、背景画像として画像が必要です。

これは、CellForRowArIndex の私のコードです。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:self options:nil];
        cell = nibLoadedCell;
    }

    for (int i = 1; i < 4; i++) {
        //----- select the correct button from the cell
        button = (UIButton *)[cell viewWithTag:(i)];


        //----- cleaning the button, drawing it's corners, borders and giving it an action
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.borderWidth = 2;
        button.layer.cornerRadius = 5;
        button.clipsToBounds = YES;


        //------ Setting up the btnImg in order to place on the buttons (if is for checking i'm not taking pictures out of the array bounds)
        UIImage *btnImg = [[UIImage alloc] init];
        if (((indexPath.row * 3) + i - 1) < photosArray.count) {
            btnImg = [photosArray objectAtIndex:((indexPath.row * 3) + i - 1)];

            //NSLog(@"I am at indexpath.row: %d",indexPath.row);
            //NSLog(@"I show image number %d in the array",[photosArray indexOfObject:btnImg]);
            NSLog(@"Expression is: %d",((indexPath.row * 3) + i - 1));
        }


        //------ Add btnImg to the button
        [button setBackgroundImage:btnImg forState:UIControlStateNormal];



        //------ Giving special tags for each button in order to recognize it later
        button.tag = imageButtonTag;
        imageButtonTag--;
    }

    return cell;
}

ご覧のとおり、コード内の for ループは、「photosArray」から画像を取得し、セル内の正しいボタンに配置する役割を担っています。

問題: 最初の ~23 個のボタンが正しい画像を取得します。その後、(少し下にスクロールする必要があります) 画像 24 を開始すると、画像が繰り返されます。画像 24 は画像 0 に相当します。画像 25 は画像 1 に相当し、以下同様です…</p>

私はすでに多くのことをチェックしました。すべてがうまく見えます!photosArray indexOfObject もチェックしましたが、数値は正しいです。

配列はこのように作られていると思うかもしれません。私もそう思いましたが、大丈夫です。for ループを削除し、代わりに次のコードを入力しました。

//----- select the correct button from the cell

button = (UIButton *)[cell viewWithTag:(1)];
 //----- cleaning the button, drawing it's corners, borders and giving it an action
[button setBackgroundImage:nil forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.layer.borderWidth = 2;
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;

//------ Setting up the btnImg in order to place on the buttons
UIImage *btnImg = [photosArray objectAtIndex:indexPath.row];

//------ Add btnImg to the button
[button setBackgroundImage:btnImg forState:UIControlStateNormal];

これにより、各セルに 1 つのボタンを持つ tableView が得られ、ボタンは適切な画像を取得します…</p>

最後に試したのは、btnImg を作成した直後の前のコードの次のコードです。

if ([btnImg isEqual:[photosArray objectAtIndex:0]]) {
    NSLog(@"Match found at: %d",[photosArray indexOfObject:btnImg]);
}

しかし、一致するものはありません(ただし、実行すると画像が同じであることがわかります)。

この問題は約 1 週間私を狂気に駆り立てています。

ありがとう!

4

1 に答える 1

0

ビュー タグをリセットすると、識別できなくなります。各コントロールを識別するためのより良い方法は、特にタグを使用してコントロールに到達するため、サブクラス化またはその他の戦略を使用することです

于 2012-12-10T20:49:31.870 に答える