0

このコードの何が問題なのかわかりませんが、ボタンを追加しなかったセル (追加する必要があるかどうかを確認する場合) で、下にスクロールしてからもう一度上にスクロールすると、ボタンが表示されます。 .

これは、テーブルビューのセル生成関数のコードです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CLASSshopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shopCell"];

    SKProduct * product = (SKProduct *) _products[indexPath.row];
    cell.titleCell.text = product.localizedTitle;
    cell.descCell.text  = product.localizedDescription;

    [_priceFormatter setLocale:product.priceLocale];
    cell.priceCell.text = [_priceFormatter stringFromNumber:product.price];


    // already yours, so no cart button
    if ([[CLASSIAPHelper sharedInstance] productPurchased:product.productIdentifier]) {
        cell.priceCell.text = @"Already yours";
    } else {
          UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
          UIImage *btn = [UIImage imageNamed:@"cart.png"];
          UIImage *btnh = [UIImage imageNamed:@"cartHover.png"];

          [buyButton setBackgroundImage:btn forState:UIControlStateNormal];
          [buyButton setBackgroundImage:btnh forState:UIControlStateHighlighted];
           buyButton.frame = CGRectMake(cell.bounds.size.width - 40,40, 24, 24);
           buyButton.tag = indexPath.row;

        [buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
         [cell.contentView addSubview:buyButton];
    }

    return cell;
}

何も特別なことに気付かなかったものをログに記録します。状態は常に検証されます。何か考えはありますか?

4

2 に答える 2

0

別の質問のコードの一部を使用して自分で解決しました。基本的に、セルにループして、すべてのボタンをクリアしてから、テストを再度実行します。

for (UIView *subview in [self.contentView subviews]) 
{
    if ([subview isKindOfClass:[UIButton class]]) 
    {
        [subview removeFromSuperview];
    }
}
于 2013-01-18T21:59:28.720 に答える
0

ボタンを追加したセルは、最終的に再利用されます。そのため、間違ったセルにボタンが表示されます;)

解決策として、ボタンのあるセルとないセルに異なる再利用識別子を使用する必要があります。

于 2013-01-29T15:17:54.730 に答える