issue with "table view with customcell"
I have a tableview with custom cells and with 5 sections. I used custom cell for "if I tap a row, the image should change". it is working fine. but, after selecting a row in first section, it also selects any one of the row in the last section. and if I scroll up and down, the first and last section's row's cell images are shifting in a random manner.
plz help...!!!
I'm a beginner in iphone development.
The following is the cellforrow code in my table view.
- (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; }
The following is the code for 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 = UIControlContentHorizontalAlignme开发者_JAVA技巧ntCenter;
[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]; }
Thanks in advance.
On your cellForRowAtIndexPath you need to set self.checked
to the appropriate value. Since the table view reuses cells that roll off the screen, you are seeing those reused cells reappear in new positions as you scroll. You are setting the content correctly but you need to set the checked value as well.
Your cell is getting reused.
The reused one has a checked
property that may still have the value from the cell's previous usage.
Make sure that you set the checked value to 'no' in cellForRowAtIndexPath
精彩评论