我有一個tableview與自定義單元格和5個部分。我使用自定義單元格「如果我點擊一行,圖像應該改變」。它工作正常。但是,在第一部分中選擇一行後,它也會選擇上一部分中的任一行。如果我上下滾動,則第一行和最後一行的單元格圖像以隨機方式移動。問題與「與customcell的表格視圖」
plz幫助... !!!
我在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]; }
預先感謝的代碼。