Search code examples
iosobjective-cipadios5

Set image on Table View Right corner


How to set UIImage on Table View right corner. i'm doing iPad app using table view and showing list of contacts. while clicking contact table view want to show right side details... here i would like to place one image which is selected on table view...

Anyone give me suggestion for this

Thanks

See my code here

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
               ![enter image description here][1]

         UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(347, 0.5, 14, 48)];
         imv.image=[UIImage imageNamed:@"activeBg.png"];
         [cell.selectedBackgroundView addSubview:imv];


}

Expected Output:

enter image description here

My output:

enter image description here

Used image:

enter image description here

See this output after changing x value:

enter image description here


Solution

  • Write both lines in viewDidLoad: method.

    arrowImage = [UIImage imageNamed:@"image"];
    [tableView setClipsToBounds:NO];
    

    Now what you have to do in didSelectRowAtIndexPath: is:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [self addArrowToIndexPath:indexPath];
    }
    

    And here the method definition:

    -(void)addArrowToIndexPath:(NSIndexPath *)indexPath
    {
        CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
    
        if (arrowView)
        {
            [arrowView removeFromSuperview];
            arrowView = nil;
        }
    
        // arrowView is an UIImageView declared globally
        arrowView  = [[UIImageView alloc] initWithImage:arrowImage];
        rect.origin.x = CGRectGetMaxX(rect);
        rect.size = arrowImage.size;
        [arrowView setFrame:rect];
        [tableView addSubview:arrowView];
    }
    

    Note: Also set the row height = arrowImage.size.height i.e. it could fit to cell height.

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return arrowImage.size.height;
    }
    

    You can directly download the sample.