1

I have an app that displays, in a tableview, an image and some details. I'm trying to make it so that, when someone touches on the image displayed in my table view, the app opens the same image in a bigger size, with the ability to navigate between them.

This is my current code:

singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgTapped:)];

singleTap.numberOfTapsRequired =1;
singleTap.numberOfTouchesRequired = 1;

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

    return [json count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

    if(cell == nil){

        [[NSBundle mainBundle] loadNibNamed:@"AgendaCell" owner:self options:nil];
        cell = self.agendaCell;

    }

    agendaCell.dateCell.text = [[json objectAtIndex:[indexPath row]] objectForKey:@"date"];
    agendaCell.enderecoCell.text = [[json objectAtIndex:[indexPath row]] objectForKey:@"endereco"];
    agendaCell.tituloCell.text = [[json objectAtIndex:[indexPath row]] objectForKey:@"titulo"];
    [agendaCell.imgCell setImageWithURL:[NSURL URLWithString:@"http://sallescds.com.br/wp-content/uploads/2012/12/xepop-300x300.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    [agendaCell.imgCell setUserInteractionEnabled:YES];
    [agendaCell.imgCell addGestureRecognizer:singleTap];


    return cell;
}

Here is the function when the image is tapped:

-(void)imgTapped:(UIGestureRecognizer *)gestureRecognizer{

    NSLog(@"detectado o click no UIImageView BITCH!");

    IMGDetailViewController *imgView = [[IMGDetailViewController alloc] initWithNibName:@"IMGDetailViewController" bundle:nil];


    [self.navigationController pushViewController:imgView animated:YES];
}

When I try this, the app just crashes.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
César Martins
  • 95
  • 1
  • 1
  • 6
  • Can you post the crash log ? – danypata May 21 '13 at 20:24
  • 1
    Do I understand the this tableview appears fine, but that it crashes when you tap on the image? If that's true, is it crashing in `viewDidLoad` of `IMGDetailViewController`? As an aside, I'm very surprised you're not setting some property of `IMGDetailViewController` so it knows which image you tapped on. – Rob May 22 '13 at 04:06
  • the crash log doesn't say anything, it just frozen the app. what I noticed that the only image that I can tap adding the tapGesture is the last one cell. – César Martins May 24 '13 at 19:16
  • I set the Cell as property, @Class AgendaCell and then I created the property. if(cell == nil){ [[NSBundle mainBundle] loadNibNamed:@"AgendaCell" owner:self options:nil]; cell = self.agendaCell; } – César Martins May 24 '13 at 19:26

1 Answers1

0

I don't know how you're setting agendaCell, but usually the line that says:

if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"AgendaCell" owner:self options:nil];
    cell = self.agendaCell;
}

would be:

if (cell == nil) {
    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"AgendaCell" owner:self options:nil];
    cell = [nibObjects objectAtIndex:0];
}

See How do you load custom UITableViewCells from Xib files?

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044