表格视图在滚动时显示不同的图像

时间:2011-06-16 04:11:42

标签: objective-c xcode ipad uitableview

我在桌面视图中加载一些图像,只需点击一下按钮。图像被正确加载到所需位置。但是当我滚动表格视图时,图像似乎发生了变化。让我详细说明只有2行可见最初在我的表格视图中,并假设图像在两行中都存在,并且连续4个图像。现在,当我向下或向上滚动表格视图时,在表格视图框架上方或下方滚动的行将显示新图像每当我滚动图像时不断变化。可能是什么原因。我正在摸着头。请帮助。我发布了我的代码的一部分: -

-(UITableViewCell*)tableView(UITableView*)
ableViewcellForRowAtIndexPath(NSIndexPath*)indexPath {

    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (cell == nil) {
        cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:AutoCompleteRowIdentifier] autorelease];

    }
    UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];
    UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];
    UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease];
    UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease];
    imageView1.tag = 1;
    imageView2.tag = 2;
    imageView3.tag = 3;
    imageView4.tag = 4;
    [cell.contentView addSubview:imageView1];
    [cell.contentView addSubview:imageView2];
    [cell.contentView addSubview:imageView3];
    [cell.contentView addSubview:imageView4];

     UIImageView * imageView;
    for ( int i = 1; i <= 4; i++ ) {
        imageView = (UIImageView *)[cell.contentView viewWithTag:i];
        imageView.image = nil;

    }

    int photosInRow;
    if ( (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) ||
        (count % 4 == 0) ) {
        photosInRow = 4;
    } else {
        photosInRow = count % 4;
    }

    for ( int i = 1; i <= photosInRow; i++ ) {
        imageView = (UIImageView *)[cell.contentView viewWithTag:i];

        [self setImage1:imageView];

    }

    return cell;
} 


-(void)setImage1:(UIImageView *)imageView
{

    UIImageView *imageView1=[[UIImageView alloc]init];

    imageView1=imageView;
    imageView1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", j]];

    j++;

}

任何帮助将不胜感激。

谢谢, 克里斯蒂

2 个答案:

答案 0 :(得分:1)

您的setImage1:方法应该被修改为接收照片索引,因为j不是跟踪当前图像的正确方法,因为cellForRowAtIndexPath:可以按任何顺序调用。

- (void)setImage1:(UIImageView *)imageView forPhotoIndex:(NSInteger)index {
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.png", index]];
}

cellForRowAtIndexPath:中的小修改,

[..]
for ( int i = 1; i <= photosInRow; i++ ) {
    imageView = (UIImageView *)[cell.contentView viewWithTag:i];
    [self setImage1:imageView forPhotoIndex:(indexPath.row * 4 + i - 1)];
}
[..]

答案 1 :(得分:0)

重用单元格时,您当前只需添加新的UImageView实例。重复使用的单元格已添加子视图,您只需添加更多。如果之前没有重复使用单元格(在if子句中),请务必仅添加新的UIImageViews

if (cell == nil) {
    cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:AutoCompleteRowIdentifier] autorelease];

    UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease];
    UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease];
    UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease];
    UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease];
    imageView1.tag = 1;
    imageView2.tag = 2;
    imageView3.tag = 3;
    imageView4.tag = 4;
    [cell.contentView addSubview:imageView1];
    [cell.contentView addSubview:imageView2];
    [cell.contentView addSubview:imageView3];
    [cell.contentView addSubview:imageView4];
}

编辑:请参阅以下有关设置实际图片的问题的评论。