2011-04-27 74 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