How to add/remove uiimage in a uitableviewcell at a specific row

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"];

}

Answers


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];

Need Your Help

Filename and path link in batch

windows bash syntax filenames ogr2ogr

So I have a line of code in .bat using ogr2ogr geospatial tool:

Error when using python textblob library tagger

python python-2.7 nltk textblob

I had the textblob library working fine for a while, but decided to install (using easy_install) an additional library (page here) claiming faster and more accurate tagging.