0

我在 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 subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; ? 有谁能够帮我 ?谢谢

4

1 回答 1

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

于 2012-06-20T13:33:03.453 回答