2012-10-31 85 views
0

我只想先說我一直在尋找這個問題,但還沒找到類似於我的問題的東西。我想要做的是用圖像改變每個單元格的背景。我有三個圖像,「上,中,下」圖像。當我放置這些背景時,tableview在滾動上會變得遲緩。這就是我的做法:iOS - 在單元格中更改背景視圖時的延遲tableview

topImage = [[UIImage imageNamed:@"table_bg_top.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 0, 10)]; 
middleImage = [[UIImage imageNamed:@"table_bg_middle.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 0, 10)]; 
bottomImage = [[UIImage imageNamed:@"table_bg_bottom.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)]; 

這三個圖像在視圖加載時僅初始化一次。然後我有以下幾點:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 
     switch (indexPath.row) { 
      case 1: 
       cell = [tableView dequeueReusableCellWithIdentifier:@"TopCell"]; 
       [(UIImageView *)[cell viewWithTag:1] setImage:topImage]; 
       ((UILabel *)[cell viewWithTag:2]).text = @"First name"; 
       ((UILabel *)[cell viewWithTag:3]).text = firstName; 
       break; 
      case 2: 
       cell = [tableView dequeueReusableCellWithIdentifier:@"MiddleCell"]; 
       [(UIImageView *)[cell viewWithTag:1] setImage:middleImage]; 
       ((UILabel *)[cell viewWithTag:2]).text = @"Last name"; 
       ((UILabel *)[cell viewWithTag:3]).text = lastName; 
       break; 
      case 3: 
       cell = [tableView dequeueReusableCellWithIdentifier:@"MiddleCell"]; 
       [(UIImageView *)[cell viewWithTag:1] setImage:middleImage]; 
       ((UILabel *)[cell viewWithTag:2]).text = @"Birth"; 
       ((UILabel *)[cell viewWithTag:3]).text = [formatter stringFromDate:birth]; 
       break; 
      case 4: 
       cell = [tableView dequeueReusableCellWithIdentifier:@"MiddleCell"]; 
       [(UIImageView *)[cell viewWithTag:1] setImage:middleImage]; 
       ((UILabel *)[cell viewWithTag:2]).text = @"Gender"; 
       ((UILabel *)[cell viewWithTag:3]).text = gender; 
       break; 
      case 5: 
       cell = [tableView dequeueReusableCellWithIdentifier:@"MiddleCell"]; 
       [(UIImageView *)[cell viewWithTag:1] setImage:middleImage]; 
       ((UILabel *)[cell viewWithTag:2]).text = @"Unit"; 
       ((UILabel *)[cell viewWithTag:3]).text = unit; 
       break; 
      case 6: 
       cell = [tableView dequeueReusableCellWithIdentifier:@"MiddleCell"]; 
       [(UIImageView *)[cell viewWithTag:1] setImage:middleImage]; 
       ((UILabel *)[cell viewWithTag:2]).text = @"Height"; 
       if([unit isEqualToString:@"mt"]) 
        ((UILabel *)[cell viewWithTag:3]).text = [NSString stringWithFormat:@"%d cm", (int)(height.floatValue*100)]; 
       else 
        ((UILabel *)[cell viewWithTag:3]).text = [NSString stringWithFormat:@"%d foot %d inches", (int) round((height.floatValue*100*CM_TO_INCH))/12, (int)round((height.floatValue*100*CM_TO_INCH)) % 12]; 
       break; 
      case 7: 
       cell = [tableView dequeueReusableCellWithIdentifier:@"BottomCell"]; 
       [(UIImageView *)[cell viewWithTag:1] setImage:bottomImage]; 
       ((UILabel *)[cell viewWithTag:2]).text = @"Weight"; 
       if([unit isEqualToString:@"mt"]) 
        ((UILabel *)[cell viewWithTag:3]).text = [NSString stringWithFormat:@"%d kg", weight.integerValue]; 
       else 
        ((UILabel *)[cell viewWithTag:3]).text = [NSString stringWithFormat:@"%d pounds",(int) round((weight.floatValue*KG_TO_LBS))]; 
       break; 
      default: 
       break; 
     } 

    return cell; 
} 

我有一個自定義單元格,具有三個不同的標識符(頂部,中間和底部)。 這是造成延遲:[(UIImageView *)[cell viewWithTag:1] setImage:image];UIImageView與單元格一樣大,以獲得背景效果。

謝謝!

回答

1

我發現了這個問題。它與:resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 0, 10),它滯後,因爲它多次繪製相同的圖像來拉伸它。所以我將不得不讓這個圖像像單元一樣大,以擺脫滯後。

如果有人找到替代解決方案,請填寫我的名單,如果它有效,我會接受它!

相關問題