0

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

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?

非常感谢,-代码

4

2 回答 2

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
    }
}
于 2012-10-13T14:24:15.273 回答
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];
}
于 2012-10-13T14:41:05.947 回答