カスタムセルと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]; }
前もって感謝します。