in the below code i am trying to show an image on the selected row and removing the image from the previously selected row. On scrolling the tableview up and down the selected image start to appear on random rows also instead of just showing on the one selected. Can anyone here please help me fix it. I am testing this on ios7 device. Thanks.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    UIImage *image = [UIImage imageNamed:@"btn_checkmark_off.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(270, 15, 17, 17);
    imageView.tag = CELL_IMGVIEW_TG;
    [cell.contentView addSubview:imageView];
}
return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

if (self.prevRowIndex != indexPath.row)
{
    NSIndexPath *pPrevIndexPath = [NSIndexPath indexPathForRow:self.prevRowIndex inSection:indexPath.section];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:pPrevIndexPath];
    UIImageView* pImgView = (UIImageView*)[cell.contentView viewWithTag:CELL_IMGVIEW_TG];
    pImgView.image = [UIImage imageNamed:@"btn_checkmark_off.png"];
    // update index
    self.prevRowIndex = indexPath.row;

}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView* pImgView = (UIImageView*)[cell.contentView viewWithTag:CELL_IMGVIEW_TG];
pImgView.image = [UIImage imageNamed:@"btn_checkmark_on.png"];

}

有帮助吗?

解决方案

Try this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(270, 15, 17, 17)];
        imageView.tag = CELL_IMGVIEW_TG;
        [cell.contentView addSubview:imageView];
    }

    UIImageView* pImgView = (UIImageView*)[cell.contentView viewWithTag:CELL_IMGVIEW_TG];
    if (self.prevRowIndex == indexPath.row) {
        pImgView.image = [UIImage imageNamed:@"btn_checkmark_on.png"];
    }
    else{
        pImgView.image = [UIImage imageNamed:@"btn_checkmark_off.png"];
    }


    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.prevRowIndex = indexPath.row;
    [self.tableView reloadData];
}

And you'd better replace prevRowIndex with selectedRowIndex which will be clear for understanding.

其他提示

try replacing this line:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

by

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top