user440096
user440096

Reputation:

Find an exact UIImageView on a cell in didSelectRowAtIndexPath

in my cellForRowAtIndexPath

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//...do cell setup etc.

            UIImage *iconthumbNail = [UIImage imageNamed:@"icon.png"];
        UIImageView * iconimgView = [[UIImageView alloc] initWithFrame:CGRectMake(265, 34, 25, 25)];
        [iconimgView setImage:iconthumbNail];
        //imgView.image = thumbNail;
        [cell addSubview:iconimgView];
        [iconimgView release];
      // add a few more UIImageViews to the cell
    }

So then in my

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
[tableView deselectRowAtIndexPath:indexPath animated:NO];

UITableViewCell * theCell = [tableView cellForRowAtIndexPath:indexPath];

    for (UIView * b in cell.subviews)
{
    if ([b isKindOfClass:[UIImageView class]])
    {
        // how do I check that its 'iconImgView' so that I can change it?
    }
}

So I add an UIImageView in my cellForRowAtIndexPath, then when that table cell is selected I want to change one of the images in the cell, 'iconImgView' but how do I identify this UIImageView from the other UIImageViews present in that cell?

Many Thanks, -Code

Upvotes: 0

Views: 1130

Answers (2)

Mick MacCallum
Mick MacCallum

Reputation: 130193

If your only goal is to change the image of the selected cell, this can be done more efficiently. All you have to do is read/write to the cells imageView property.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *theCell = [tableView cellForRowAtIndexPath:indexPath];
    [[theCell imageView] setImage:[UIImage imageNamed:@"someImage.jpg"]];//altering the existing image
    UIImageView *myImageView = theCell.imageView;//read the image property of the cell
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Upvotes: 0

Martol1ni
Martol1ni

Reputation: 4702

Instead of using that approach, I would assign a tag to the UIImageView you are looking for.

iconimgView.tag = 1;

Then in the didSelectRowAtIndexPath method, use this:

UIImageView *iconimgView = (UIImageView *)[cell viewWithTag:1];

If you still want to do it your way, you can do like this:

if ([b isKindOfClass:[UIImageView class]]) {
    UIImageView *myView = (UIImageView *)b;
    if ([b.image isEqual:[UIImage imageNamed:@"yourImageName"]]) {
        // Do your stuff
    }
}

Upvotes: 2

Related Questions