2011-08-20 48 views
0

我使用一個UITableView顯示開車快的後果的一些信息:什麼罰款將是等我也用,你在你的駕駛執照獲得一些紅色的「點」。因爲這可能是介於0 - 每一行我做了以下6:添加動態的UIImageView在UITableViewCell中

- (UITableViewCell *)tableView:cellForRowAtIndexPath: 

// Code if no cell is available in the que 

// End of that code 

UIImage *seniorImage = [UIImage imageNamed:@"ticket_dot.png"]; 

if (seniorDots == 0) { 

} else { 
    for (int i = 0; i < seniorDots; i++) { 
     UIImageView *seniorImageView = [[UIImageView alloc] initWithFrame:CGRectMake(40.0 + (i * 15.0), 40.0, 15.0, 40.0)]; 
     seniorImageView.image = seniorImage; 
     seniorImageView.contentMode = UIViewContentModeCenter; 
     [cell.contentView addSubview:seniorImageView]; 
     [seniorImageView release]; 
    } 
} 

seniorDots是一個變量,我從plist文件中得到。該代碼正在按預期工作,但您可能會看到我無法刪除它們。所以我第一次打開的tableview我看到預期的觀點,但如果我再次向上滾動代碼只是保持對小區追加的UIImageViews,不刪除舊的。如何在添加新的點之前引用這些點以將它們從屏幕上移除?

回答

1

你可以把下面的代碼你的cellForRowAtIndexPath方法中:

//刪除其他數據

for (UIImageView *img in cell.contentView.subviews) { 
     if ([img isKindOfClass:[UIImageView class]]) { 
      [img removeFromSuperview]; 
     } 
    } 

我希望這將是對你有所幫助。如有任何困難,請告知我。

+0

iOS開發:謝謝,這是怎樣的一個骯髒的,但。如果我在小區有其他的UIImageViews這並沒有改變它不會做的伎倆正常。但在這種情況下,它的工作原理,但你得到了在代碼中的一些錯誤,但我明白你的意思。如果沒有其他人有更好的解決方案,我會接受它,因爲它有效。 – LuckyLuke

+0

感謝您的反饋。 –

2
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

UIImageView *imgView; 
if(cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero   reuseIdentifier:CellIdentifier] autorelease]; 

imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,0,100,62)]; 
[imgView setImage:[UIImage imageNamed:@"img.png"]]; 
imgView.tag = 55; 
[cell.contentView addSubview:imgView]; 
[imgView release]; 
} 
else 
{ 
    imgView = (id)[cell.contentView viewWithTag:55]; 
} 
相關問題