1

カスタムセルと5つのセクションを持つテーブルビューがあります。「行をタップすると画像が変わる」というカスタムセルを使用しました。それは正常に動作しています。ただし、最初のセクションで行を選択した後、最後のセクションのいずれかの行も選択します。上下にスクロールすると、最初と最後のセクションの行のセル画像がランダムに移動します。

助けてください...!!!

iphone開発初心者です。

以下は、テーブル ビューの cellforrow コードです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *kCustomCellID = @"MyCellID";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
    cell = (CustomCell *) [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID]autorelease];
}
NSArray *localperson = [personArray objectAtIndex:indexPath.section];
cell.textLabel.text = [localperson objectAtIndex:indexPath.row]; 
return cell; }

以下は、CustomCell のコードです。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {   

if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
    self.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    // cell's check button
    checkButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    checkButton.frame = CGRectZero;
    checkButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    checkButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [checkButton addTarget:self action:@selector(checkAction:) forControlEvents:UIControlEventTouchDown];
    checkButton.backgroundColor = self.backgroundColor;
    [self.contentView addSubview:checkButton];
}
return self; }


- (void)layoutSubviews  { [super layoutSubviews];
CGRect contentRect = [self.contentView bounds];
CGRect frame = CGRectMake(contentRect.origin.x + 40.0, 8.0, contentRect.size.width, 30.0);
self.textLabel.frame = frame;
// layout the check button image
UIImage *checkedImage = [UIImage imageNamed:@"checked.png"];
frame = CGRectMake(contentRect.origin.x + 10.0, 12.0, checkedImage.size.width, checkedImage.size.height);
checkButton.frame = frame;
UIImage *image = (self.checked) ? checkedImage: [UIImage imageNamed:@"unchecked.png"];
UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[checkButton setBackgroundImage:newImage forState:UIControlStateNormal];   }

前もって感謝します。

4

2 に答える 2

1

cellForRowAtIndexPathself.checkedで、適切な値に設定する必要があります。テーブルビューは画面からロールオフするセルを再利用するため、スクロールすると、再利用されたセルが新しい位置に再表示されます。コンテンツを正しく設定していますが、チェックした値も設定する必要があります。

于 2011-08-30T14:30:12.733 に答える
1

あなたの細胞は再利用されています。

再利用されたものにはchecked、セルの以前の使用からの値がまだある可能性があるプロパティがあります。

チェックした値を「いいえ」に設定したことを確認してくださいcellForRowAtIndexPath

于 2011-08-30T14:32:49.850 に答える