【问题标题】:Table View displaying different images while scrolling滚动时显示不同图像的表格视图
【发布时间】:2011-06-16 04:11:41
【问题描述】:

我通过单击按钮在我的表格视图中加载一些图像。图像加载良好并且在所需的位置。但是当我滚动表格视图时,图像似乎发生了变化。让我详细说明只有 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++;

}

我们将不胜感激。

谢谢, 克里斯蒂

【问题讨论】:

  • setImage1:是如何实现的?

标签: objective-c xcode ipad uitableview


【解决方案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)];
}
[..]

【讨论】:

  • 非常感谢您的出色工作,现在您能告诉我在表格视图中选择图像的方法吗?当我单击图像时,整个单元格都会被选中
【解决方案2】:

重用单元格时,您当前只需添加新的 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];
}

编辑:有关设置实际图像的问题,请参阅下面的 cmets。

【讨论】:

  • 之前我只在 if(cell==nill) 部分内完成了该操作,但结果也相同,即您现在更改了代码,但它仍然无法正常工作
  • 我假设你在某个地方跟踪countvariable?你怎么做到这一点?如果你打电话给[self setImage1:imageView];,那里的计数值是否用于设置正确的图像?如果是,请考虑在向上滚动时调用(UITableViewCell*)tableView(UITableView*) ableViewcellForRowAtIndexPath(NSIndexPath*)indexPath 的顺序不是按顺序,所以这可能是您的问题。
  • 是的,它显示了正确的图像,所以我必须做些什么来保持滚动顺序
猜你喜欢
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
相关资源
最近更新 更多