选定的UITableViewCell会更改Scroll上的背景

时间:2011-04-27 07:33:30

标签: objective-c cocoa-touch ios4 uitableview

我有一个透明背景颜色的UITableView,每个单元格都有自定义背景视图和灰色选择样式。选择工作正常,但当我选择并向上或向下拖动tableview时,单元格将其背景更改为透明而不是自定义。我该怎么办呢?

编辑:André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;
}

1 个答案:

答案 0 :(得分:1)

很抱歉,这些不一定是您的问题的原因,但首先要做到这些:

  • 不需要else块(或者你只是把它放在那里用于调试目的?)
  • selectionStyle块中应用if和背景应该足够了(除非你在其他地方更改它们)
  • restaurantNameLabel可能应该添加到单元格contentView而不是直接作为子视图
  • 你正在泄漏restaurantNameLabel和bgView;完成后添加[bgView release][restaurantNameLabel release]

另外,您是否因任何原因使用UIImageView?使用UIView或者甚至只是应用backgroundColor可能就足够了。

相关问题