How to get selected image and navigate to detail view from UITableViewCell (Custom cell) load by nib in UITableView

StackOverflow https://stackoverflow.com/questions/22783661

Question

I have an UITableViewCell and in this cell i have 3 UIImageView.This is my custom cell loaded in my UITableView by nib file.I want to get selected image when i tapped on image and navigate in detailViewController. for that i added UIGestureRecognizer in all 3 ImageView at the time of creating cell. This is my demo application because I'm new in iphone developing and my application is like photo gallary when tap on photo navigate the page and display that photo in big size. i have to use UItableView and custom cell so please don't give other suggestions because i'm in training and i have to use this without it i already done. but in UITableView when i tap on image the whole cell is select and my method given on @selecter is not call.

Code:-

MasterViewController.m file

@implementation MasterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Gallery", @"Gallery");
    }
    return self;
}


- (void)viewDidLoad
{
    NSLog(@"Enter in viewDidLoad method");

    //alloc init array with array of .png and .jpg file names.
    imgFileName = [[NSArray alloc]initWithArray:[[[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle]bundlePath] error:nil]filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH[cd] '.png'","self ENDSWITH[cd] '.jpg'"]]];


    NSLog(@"Image file name = %@",imgFileName);
    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    NSLog(@"Enter in viewDidLoad method");

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return imgFileName.count/3+1;

}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    

    NSLog(@"Enter Cell creating process.....");


    // Similar to UITableViewCell, but 
    static NSString *CellIdentifier = @"Cell";

    Customcell *cell = (Customcell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"Customcell" owner:self options:nil];

        for (id currentobject in topLevelObject)
        {          
            if ([currentobject isKindOfClass:[UITableViewCell class]]) 
            {
                NSLog(@"Enter in IF for Creating cell");
                cell = (Customcell *) currentobject;


                NSLog(@"cell reuseIdentifier = %@",cell.reuseIdentifier);
                break;
            }       
        }
    }


    NSLog(@"Row == %d",indexPath.row);

    if ((indexPath.row*3) == imgFileName.count)
    {
        NSLog(@"Counter in 1st if == %d ",(indexPath.row*3));
        return NULL;
    }             

    NSLog(@"Counter out 1st if == %d ",(indexPath.row*3));
    [cell.img1 setImage:[UIImage imageNamed:[imgFileName objectAtIndex:(indexPath.row*3)]]];
    [cell.img1 setTag:indexPath.row*3];
    [cell.img1 addGestureRecognizer:[[UIGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapped:)]];   


    if (((indexPath.row*3)+1) == imgFileName.count)
    {
        NSLog(@"Counter in 2nd if == %d ",(indexPath.row*3)+1);
        return cell;
    }          
    NSLog(@"Counter out 2nd if == %d ",(indexPath.row*3)+1);
    [cell.img2 setImage:[UIImage imageNamed:[imgFileName objectAtIndex:(indexPath.row*3)+1]]];

    [cell.img2 setTag:(indexPath.row*3)+1];
    [cell.img2 addGestureRecognizer:[[UIGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapped:)]];

    if (((indexPath.row*3)+2) == imgFileName.count)
    {
        NSLog(@"Counter in 3rd if == %d ",(indexPath.row*3)+2);
        return cell;
    }
    NSLog(@"Counter out 3rd if == %d ",(indexPath.row*3)+2);
    [cell.img3 setImage:[UIImage imageNamed:[imgFileName objectAtIndex:(indexPath.row*3)+2]]];
    [cell.img3 setTag:(indexPath.row)+2];
    [cell.img3 addGestureRecognizer:[[UIGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapped:)]];


    NSLog(@"Exit cell creating process...");

    return cell;

}


-(void)imageTapped:(UITapGestureRecognizer *)sender
{
    NSLog(@"Enter in imageTapped method");

    DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];

    UIImageView *img = (UIImageView *)sender.view;//cast sender obj. to ImageView

    NSLog(@"The Selected tag is = %d",img.tag);

    detailViewController.imageD = img.image;
    [img release];       
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];

    NSLog(@"Exit from imageTapped method");

}

CustomCell.m file

#import "Customcell.h"

@interface Customcell ()

@end

@implementation Customcell
@synthesize img1,img2,img3;


@end

DetailViewController.m file

- (void)viewDidLoad
{
    imageViewD = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imageD.size.width,imageD.size.height)];

    [imageViewD setImage:imageD];
    //[self.view addSubview:imageViewD];
    [super viewDidLoad];

    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    if (imageViewD.frame.size.height > self.view.frame.size.height)
    {
        scroll.contentSize = CGSizeMake(imageViewD.frame.size.width+50, imageViewD.frame.size.height+50);
    }
    [scroll addSubview:imageViewD];
    [self.view addSubview:scroll];

}

i hope this code will help you and you can give me answer or suggestion quickly.

Was it helpful?

Solution

I think the issue is because you havent specified what type of gesture is to be applied, there are different types of gestures

Please try this

UITapGestureRecognizer *tapped=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapped:)]; [cell.img1 addGestureRecognizer:tapped];

Similarly add UITapGestureRecognizer for all the other 2 imageviews

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top