2010-10-16 50 views
0

我正在使用表格視圖和添加小圖像(交叉箭頭一個接受箭頭圖像),以每個單元格的右側爲基礎,如果該選項是正確的或不....刪除tableview的所有子圖像視圖

....現在我的表重新加載很多次....由於在每個單元格上重複的圖像....

我的意思是,如果cell_1有接受image....表重新加載,如果cell_1的值發生了變化,它需要cross_arrow圖像...那麼這兩個圖像都可見那裏...(新的一個以上舊的).....

我認爲這是由於子視圖被添加到它最後這是不會被刪除的時間..

請告訴我如何刪除舊形象

這裏是我的一些代碼,這增加了圖像的細胞......在的tableView

UIImageView *image =[[UIImageView alloc]initWithFrame:CGRectMake(260,5,40,40)]; 
if(correct) 
    [image setImage:[UIImage imageNamed:@"right.png"]]; 
else 
    [image setImage:[UIImage imageNamed:@"wrong.png"]]; 
image.autoresizingMask; 
image setBackgroundColor:[UIColor clearColor]]; 
[cell addSubview:jimage]; 
[jobStatus release]; 

的委託方法請注意,我必須只使用我的圖像,而不是表的附件類型。

並且在我的表視圖(在運行時創建)中沒有固定數量的行。

因此我也不能使用類imageView。

請幫助

回答

1

圖像視圖使用,當你出列細胞像一個類似的方法:問問小區的ImageView的,如果它不存在創建它。

#define kTagCellImageView 42 

- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"EditorCell"; 

    UITableViewCell *cell = [tableView_ dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    UIImageView *imageView = [cell viewWithTag:kTagCellImageView]; 
    if (imageView == nil) { 
     imageView = [[UIImageView alloc]initWithFrame:CGRectMake(260,5,40,40)]; 
     imageView.backgroundColor = [UIColor clearColor]; 
     imageView.tag = kTagCellImageView; 
     [cell addSubview:imageView]; 
    } 
    if (correct) 
     [imageView setImage:[UIImage imageNamed:@"right"]]; 
    else 
     [imageView setImage:[UIImage imageNamed:@"wrong"]]; 
    cell.textLabel.text = [[textLabels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
    return cell; 
} 
+0

+ 1 - 謝謝你的作品..... – Saawan 2010-10-18 05:21:55

1

設置UIImageView的標記,以便您可以

[imageView setTag:10]; 

稍後訪問它,那麼你可以改變從表視圖單元格的圖像視圖的圖像通過調用

[[tableViewCell viewWithTag:10] setImage:[UIImage imageNamed:@"wrong.png"]]; 

另一種解決方案:在單元格的右側顯示圖像,您可以設置表格視圖單元格的附件視圖

[tableViewCell setAccessoryView:imageView]; 

,然後讓你訪問與

[(UIImageView*)tableViewCell.accessoryView setImage:...]; 
+0

+ 1 - 我想嘗試混淆me..please給一些更多的光線第二個,但最後一行... – Saawan 2010-10-16 11:05:39

+0

首先,我們的表視圖單元格的附屬視圖設置了圖片查看,然後我們可以通過accessoryView屬性訪問圖像視圖。因爲accessoryView是一個UIView,所以我們必須將它轉換爲UIImageView *,以便我們可以使用setImage:圖像,而無需編譯器警告。 – brutella 2010-10-16 12:29:06