在didSelectRowAtIndexPath中的单元格上查找精确的UIImageView

时间:2012-10-13 14:17:33

标签: iphone objective-c ios

在我的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
    }

那么在我的

- (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?
    }
}

所以我在我的cellForRowAtIndexPath中添加了一个UIImageView,然后当选择该表格单元格时,我想更改单元格中的一个图像'iconImgView'但是如何从该单元格中存在的其他UIImageViews中识别出这个UIImageView?

非常感谢, -code

2 个答案:

答案 0 :(得分:2)

我会将标签分配给您正在寻找的UIImageView,而不是使用该方法。

iconimgView.tag = 1;

然后在didSelectRowAtIndexPath方法中,使用:

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

如果你仍然想按照自己的方式去做,你可以这样做:

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

答案 1 :(得分:0)

如果您的唯一目标是更改所选单元格的图像,则可以更有效地完成此操作。您所要做的就是读取/写入单元格imageView属性。

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