如果在iPhone sdk的单元格背景图像中添加每个单元格,如何将选择样式设置为蓝色?

时间:2010-09-13 10:33:24

标签: iphone objective-c uitableview

我将选择样式设置为蓝色时遇到问题,我添加了单元格背景图像,这样我就无法将选择样式设置为蓝色,如果我们将图像添加到每个单元格,我该怎么做呢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[myTableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease];
        UIView* elementView =  [ [UIView alloc] initWithFrame:CGRectMake(5,5,300,480)];
        elementView.tag = 0;
        [cell.contentView addSubview:elementView];
        [elementView release];
    }

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    UIView* elementView  = [cell.contentView viewWithTag:0];
    for(UIView* subView in elementView.subviews)
    {
        [subView removeFromSuperview];
    }

    UIImageView* cellBGImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
    cellBGImageView.image=[UIImage imageNamed:@"cellbg.png" ];
    [elementView addSubview:cellBGImageView];
    [cellBGImageView release];

    Customer *objectForCustomer=[customerList objectAtIndex:indexPath.row];

    UILabel *nameLable =[[UILabel alloc] initWithFrame:CGRectMake(10, 7, 280, 20)];
    [nameLable setFont:[UIFont boldSystemFontOfSize:17]];
    nameLable.textAlignment=UITextAlignmentLeft;
    //nameLable.textColor = [UIColor blackColor];
    nameLable.numberOfLines = 0;
    nameLable.tag=2;
    nameLable.backgroundColor = [UIColor clearColor];
    nameLable.text=objectForCustomer.customerName;
    [elementView addSubview:nameLable];
    [nameLable release];

return cell;
}

3 个答案:

答案 0 :(得分:0)

当您使用图像覆盖整个单元格时,您无法看到所选单元格。 你必须实现自定义单元来处理(或显示)某个单元被选中。

答案 1 :(得分:0)

我知道当你有一个自定义单元格时,你可以在IB中指出它。不知道代码是否可行(我猜它应该)。

尝试在代码中使用它:

[cell setBackgroundView:cellBGImageView.image];

我怀疑它是否有效,但值得一试。

答案 2 :(得分:0)

嘿,我在您的代码中发现了一个问题,即您将单元格的selectionStyle设置为none。

问题与图像无关,但问题在于代码。

我正在为您提供便利的代码重写代码。请检查出来

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

{ UITableViewCell *cell = (UITableViewCell )[myTableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyIdentifier"] autorelease]; 
        UIView elementView = [ [UIView alloc] initWithFrame:CGRectMake(5,5,300,480)]; 
        elementView.tag = 0;
        [cell.contentView addSubview:elementView];
        [elementView release]; 
    }

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
//THIS IS THE LINE I HAVE COMMENTED THAT WAS CREATING THE PROBLEM
//  cell.selectionStyle=UITableViewCellSelectionStyleNone;

    UIView* elementView = [cell.contentView viewWithTag:0];
    for(UIView* subView in elementView.subviews)
    { 
        [subView removeFromSuperview]; 
    }

    UIImageView* cellBGImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)]; 
    cellBGImageView.image=[UIImage imageNamed:@"cellbg.png" ]; 
    [elementView addSubview:cellBGImageView];
    [cellBGImageView release];

    Customer *objectForCustomer=[customerList objectAtIndex:indexPath.row];

    UILabel *nameLable =[[UILabel alloc] initWithFrame:CGRectMake(10, 7, 280, 20)];
    [nameLable setFont:[UIFont boldSystemFontOfSize:17]]; 

     nameLable.textAlignment=UITextAlignmentLeft; //nameLable.textColor = [UIColor blackColor]; nameLable.numberOfLines = 0;
      nameLable.tag=2; 
      nameLable.backgroundColor = [UIColor clearColor]; 
      nameLable.text=objectForCustomer.customerName; 
     [elementView addSubview:nameLable]; 
     [nameLable release];

    return cell; 
}

快乐的编码......