开发者

Selected UITableViewCell changes background on Scroll

I have a UITableView with transparent background color, each of its cells have a custom background view, and gray selection style. The selection works fine, but when i select and drag the tableview up or down, the cell changes its background to transparent instead of the custom one. What shall i look to fix it?

EDIT: Source Code as requested by 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;
}


Sorry, these aren't necessarily the cause for your problem, but start by doing these:

  • no need for the else block (or did you just put it there for debugging purposes?)
  • it should be enough to apply the selectionStyle and background inside the if block (unless you're changing them somewhere else)
  • the restaurantNameLabel should probably be added to the cell's contentView and not directly as a subview
  • you're leaking restaurantNameLabel and bgView; add [bgView release] and [restaurantNameLabel release] after you're done with them

Also, are you using a UIImageView for any reason in particular? It'd probably be enough to use a UIView, or even just to apply a backgroundColor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜