2012-06-20 14 views
0

我已經在GMGridView中添加了UIImageView,因爲我想在網格中顯示圖像。在網格中添加imageview後,所有工作都完美無缺。我想在第一行&第一列顯示不同的圖像。因此,我在與索引進行比較後設置了該圖像。 但是當我向下滾動&時,它會將圖像從第一列更改爲第二或第三列。第6行或第7行也顯示相同的圖像。這有什麼問題?這裏是我的代碼在Imageviews中的圖像在SwingGGridView上下切換iphone

- (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

嘗試使用代碼

- (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的單元格,然後它永遠不會再因爲控制進行去除後新增由於實例cell的存在而未進入條件if (!cell) {}。 希望這可以幫助你。

+0

這工作對我來說,沒有別的什麼......謝謝! – codeburn