滚动GridView后,图像视图中的图像进行交换;在iphone中

时间:2012-06-20 12:40:08

标签: ios5 reusability gmgridview

我在GMGridView中添加了UIImageView,因为我想在网格中显示图像。在网格中添加imageview后,一切都很完美。我希望在第一行和第一行显示不同的图像。第一栏。所以我在与索引进行比较后设置了该图像。 但是当我向上滚动时向下,它将图像从第一列更改为第二列或第三列。同样的图像也显示在第6行或第7行。这出了什么问题?这是我的代码

    - (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
    CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];

    GMGridViewCell *cell = [gridView dequeueReusableCell];

    UIImageView *imageView = nil;
    if (!cell) 
    {
        cell = [[GMGridViewCell alloc] init];
        cell.deleteButtonIcon = [UIImage imageNamed:@"close_x.png"];
        cell.deleteButtonOffset = CGPointMake(-15, -15);

        imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];

        cell.contentView = imageView;

    }

    NSLog(@"Project Index value:- %d",index);
    if (index == 0) {
        imageView.image = [UIImage imageNamed:@"add.png"]; 
    }
    else{
        imageView.image = [UIImage imageNamed:@"face.png"]; 
    }

    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];    
    return cell;
}

我还需要[[cell.contentView子视图] makeObjectsPerformSelector:@selector(removeFromSuperview)]; ? 有谁能够帮我 ?感谢

1 个答案:

答案 0 :(得分:1)

尝试使用代码

- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index {
  CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
  GMGridViewCell *cell = [gridView dequeueReusableCell];

UIImageView *imageView = nil;
if (!cell) 
{
    cell = [[GMGridViewCell alloc] init];
    cell.deleteButtonIcon = [UIImage imageNamed:@"close_x.png"];
    cell.deleteButtonOffset = CGPointMake(-15, -15);

    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    NSLog(@"Project Index value:- %d",index);
    if (index == 0) {
       imageView.image = [UIImage imageNamed:@"add.png"]; 
    }
    else{
       imageView.image = [UIImage imageNamed:@"face.png"]; 
    }
       [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];    
    cell.contentView = imageView;

}
return cell;

}

我认为问题可能是您正在使用makeObjectsPerformSelector删除单元格,然后在删除后永远不会再添加单元格,因为控件可能因为存在而无法进入条件if (!cell) {}实例cell。 希望这会对你有所帮助。