2010-04-19 25 views
3

我有一個問題,將UITableView的背景設置爲UIImageView(請參閱下面爲什麼我這樣做),一旦設置了視圖,它工作正常,並使用UITableView滾動,但它隱藏表視圖的元素。UITableViewCells出現在backgroundView後

我需要有一個UIImageView作爲UITableView的背景。我知道這之前已經回答,但答案是使用:

[UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]]; 

或者類似的東西(我需要使用):

UIImageView *background = [MainWindow generateBackgroundWithFrame:tableView.bounds]; 
[tableView addSubview:background]; 
[tableView sendSubviewToBack:background]; 

我之所以需要使用後者是因爲

+ (UIImageView *) generateBackgroundWithFrame: (CGRect)frame { 
    UIImageView *background = [[[UIImageView alloc] initWithFrame:frame] autorelease]; 
    background.image = [UIImage imageNamed:@"globalBackground.png"]; 
    [background.layer setMasksToBounds:YES]; 
    [background.layer setCornerRadius:10.0]; 
    [background.layer setBorderColor:[[UIColor grayColor] CGColor]]; 
    [background.layer setBorderWidth:3.0]; 

    return background; 
} 

請注意:我明白牛逼我generateBackgroundWithFrame方法,這需要一個大的圖像,並繪製圍繞圖像指定尺寸的邊框,剪輯圖像的剩餘部分這可能會影響性能,但我沒有足夠的資源去製作應用程序中每個潛在屏幕的圖像。請不要盲目回答這個問題「你不應該這樣做」的迴應。我知道這可能是錯誤的做法。

如何顯示我的UITableView控件元素?在我的委託中有沒有我做錯的事情?這裏是一個簡化的版本:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = 
     [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] 
       initWithFrame:CGRectMake(20, 20, 261, 45) 
       reuseIdentifier:CellIdentifier] autorelease]; 

     cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

     UIImage *rowBackground;   
     NSString *imageName = @"standAloneTVButton.png";  
     rowBackground = [UIImage imageNamed:imageName];  

     UITextView *textView = 
       [[UITextView alloc] initWithFrame:CGRectMake(0, 5, 300, 200)]; 

     textView.backgroundColor = [UIColor clearColor]; 
     textView.textColor = [UIColor blackColor]; 
     textView.font = [UIFont fontWithName:@"Helvetica" size:18.0f]; 

     Purchase *purchase = 
       [[PurchaseModel productsPurchased] objectAtIndex:indexPath.row]; 

     textView.text = [purchase Title]; 

     selectedTextView.text = textView.text; 


     UIImageView *normalBackground = 
       [[[UIImageView alloc] init] autorelease]; 

     normalBackground.image = rowBackground; 

     [normalBackground insertSubview:textView atIndex:0]; 

     cell.backgroundView = normalBackground; 
     [textView release]; 
    } 

    return cell; 
} 

回答

2

所以回答這個問題迄今標籤添加到背景視圖,然後執行以下操作:

- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)path { 
    [tableView sendSubviewToBack:[tableView viewWithTag:BACKGROUNDVIEW_TAG]]; 
} 

我真的不喜歡這一點,因爲我正在爲每個細胞做這件事,但我似乎無法找到一種方法。

+0

我已經瘋狂了這個問題一整天!目前我還沒有找到更好的解決方案。 非常感謝! – Claus 2012-08-07 10:11:47

相關問題