2011-04-27 42 views
0

我有一個透明背景顏色的UITableView,它的每個單元都有自定義背景視圖和灰色選擇樣式。選擇工作正常,但是當我選擇並向上或向下拖動桌面視圖時,單元格將其背景更改爲透明而非自定義。我該怎樣修復它?所選的UITableViewCell更改Scroll背景

編輯:源代碼的要求安德烈Morujão

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


    static NSString *CellIdentifier = @"Cell"; 

    RestaurantInfo *restaurantInfo = [requestBean.restaurantArray objectAtIndex:indexPath.row]; 


    UITableViewCell *cell = [tableView 

          dequeueReusableCellWithIdentifier:CellIdentifier]; 




    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] 

       initWithStyle:UITableViewCellStyleDefault 

       reuseIdentifier:CellIdentifier] autorelease]; 

    } 
    else { 
     cell = nil; 

     cell = [[[UITableViewCell alloc] 

       initWithStyle:UITableViewCellStyleDefault 

       reuseIdentifier:CellIdentifier] autorelease]; 

    } 

    [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; 

    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; 
    [bgView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"table_cell_bg.png"]]]; 
    [cell setBackgroundView:bgView]; 


    UILabel *restaurantNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 310, 15)]; 
    [restaurantNameLabel setFont:[UIFont boldSystemFontOfSize:16]]; 
    [restaurantNameLabel setText:restaurantInfo.restaurantName]; 
    [restaurantNameLabel setBackgroundColor:[UIColor clearColor]]; 
    [restaurantNameLabel setUserInteractionEnabled:NO]; 
    [cell addSubview:restaurantNameLabel]; 


    return cell; 
} 
+0

你可以粘貼你的tableView:cellForRowAtIndexPath:? – 2011-05-03 09:11:39

+0

我已經像你說的那樣用方法 – 2011-05-03 09:37:32

回答

1

對不起,這不一定是你的問題的原因,但是通過做這些開始:

  • 沒有必要爲else塊(或者你只是爲了調試的目的而把它放在那裏?)
  • 它應該足以應用selectionStyleif塊內的背景(除非你改變在別的地方)
  • restaurantNameLabel或許應該被加入到細胞的contentView,並不能直接作爲子視圖
  • 你泄露restaurantNameLabel和bgView;添加[bgView release][restaurantNameLabel release]

此外,您使用的任何原因特別是UIImageView他們做後?這可能足以使用UIView,或者甚至只是應用backgroundColor

+0

編輯了這個問題,非這些改變解決了這個問題,還有其他線索嗎? – 2011-05-03 10:15:13

+0

將backgroundcolor應用於圖案圖像的單元格也無濟於事 – 2011-05-03 10:16:04

+0

我忘了提及:不要每次都添加UILabel;只將它添加到if塊中,然後檢索它(使用viewWithTag:或類似的東西),並只設置if塊之外的文本。另外,你可以發佈前後的樣子截圖嗎? (+更新您的代碼到最新版本) – 2011-05-03 10:29:06