UIImageView始终在自定义uitableviewcells中显示相同的图像

时间:2011-06-07 17:14:11

标签: image uitableview uiimageview

在uitableview中我至少有一行,然后我添加了一个可变数量的其他行,其单元格是一个自定义单元格,使用uiimageview以编程方式创建。 uiimageview设置为本地图像。正确检索此图像,但我看到所有行上都重复相同的图像。

我尝试在

中的setImage之后使用[myImageView setNeedsDisplay];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  

任何提示?

编辑

一些代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CellWithTextIdentifier";
UITableViewCell *cell;
static NSString *cellIdentifier2 = @"CellWithImageIdentifier";
UITableViewCell *cell2;
UIImageView *logImageView;

switch (indexPath.row) {
    case 0:
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"MyCostumCell" owner:self options:nil];
            cell = myCustomCell;
            self.myCustomCell = nil;
        }

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

        break;
    default:

        cell2 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
        if (cell2 == nil) {
            cell2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier2] autorelease];
            logImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(100.0, 20.0, 100.0, 100.0)] autorelease];
            cell2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
            [cell2.contentView addSubview:logImageView];
        }


        cell2.selectionStyle = UITableViewCellSelectionStyleNone;

                    NSMutableArray *images = [[NSMutableArray alloc] init];

                    // here I fill the array images. It's properly filled with
                    // objects which have a path property with the relative path of the image

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *absolutePath = [documentsDirectory stringByAppendingPathComponent:[[images objectAtIndex:indexPath.row-1] path]];

        UIImage *image = [UIImage imageWithContentsOfFile:absolutePath];

        [logImageView setImage:image];
        //[logImageView setNeedsDisplay];

        [images release];
        return cell2;

        break;

}   

}

编辑2:已解决

问题是logImageView。如果重用cell2,则不会初始化uiimageview。 解决方案是:

  cell2 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
        if (cell2 == nil) {
            cell2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier2] autorelease];
            logImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(100.0, 20.0, 100.0, 100.0)] autorelease];
                  logImageView.tag = 12;
            cell2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
            [cell2.contentView addSubview:logImageView];
        }
      } else {
            logImageView = (UIImageView *)[cell2.contentView viewWithTag:12];
        }

0 个答案:

没有答案