views:

39

answers:

3

Hi Guys,

I had a problem to set the selection style as blue, I have added the cell back ground image so that i can not set the selection style as blue how can i make it if we added images to each cell.

  • (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; }

A: 

When you are covering entire cell with image, you cannot see the cell selected. you have to implement custom cell for that to handle (or show) that some cell is selected.

Satyam svv
A: 

I know that when you have a custom cell, you can point it in IB. Don't really know if it is possible from code (I guess it should).

Try using this in your code:

[cell setBackgroundView:cellBGImageView.image];

I am sceptic that it works, but worth a try.

Regards,
Paul Peelen

Paul Peelen
A: 

Hey I have sen a problem in your code that you are setting the selectionStyle of cell as none.

The problrm is not with the image but the problem is with the code.

I am rewriting the code for you convinience. Please check it out

-(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; 
}

hAPPY cODING...

Suriya