0

I have a tap and hold the event in the list (UITableView) of my application:

ViewDidLoad PlayerViewController.m:

UILongPressGestureRecognizer *agendarProg = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(agendarPrograma:)];
agendarProg.minimumPressDuration = 0.5; //segundos
agendarProg.delegate = self;
[self.tableView addGestureRecognizer:agendarProg];

function agendarPrograma in PlayerViewController.m:

-(void)agendarPrograma:(UILongPressGestureRecognizer *)gestureRecognizer {

    CGPoint ponto = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:ponto];

    cell = [self.tableView cellForRowAtIndexPath:indexPath];

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {

        if (cell.imageAgendamento.hidden == true) {
            cell.imageAgendamento.hidden = false;
            NSString *horaPrograma = [ NSString stringWithFormat:@"%@",[[results objectAtIndex:indexPath.row] objectForKey:@"hora" ]];
            [self addNotification:horaPrograma];
            UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Agendamento" message:@"Programa agendado" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
            [alert1 show];
            [self performSelector:@selector(dismiss:) withObject:alert1 afterDelay:1.5];

        } else {
            cell.imageAgendamento.hidden = true;
            [self deleteNotification];
            UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Agendamento" message:@"Agendamento cancelado" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
            [alert1 show];
            [self performSelector:@selector(dismiss:) withObject:alert1 afterDelay:1.5];
        }

    } else {
        return;
    }
}

PROBLEM: when I use tap and hold him add the image on the line I chose but also add in others. I need it to add just where I'm using tap and hold. add in row choose but add other imagem in list random.

EXAMPLE: I used tap and hold in "Sorrindo pra vida" hidden image equal a false, but in "Musicas Marianas" the image show too

enter image description here

Felipe Xavier
  • 165
  • 2
  • 13

2 Answers2

0

You can set multi selected of table to false and get correct cell by delegate tableview didSelectRow atIndexPath.

Trung Phan
  • 923
  • 10
  • 18
0

Just store indexPath as global, like _indexPath:

-(void)agendarPrograma:(UILongPressGestureRecognizer *)gestureRecognizer {
    CGPoint ponto = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:ponto];
    _indexPath = indexPath;
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
     [self.tableView reloadData];
    } else {
        return;
    }
}

and in: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath you implement this:

if([indexPath compare:_indexPath] == NSOrderedSame) {
   cell.imageAgendamento.hidden = NO;
}else{
   cell.imageAgendamento.hidden = YES;
}

Hope this could help.

Nghia Luong
  • 790
  • 1
  • 6
  • 11
  • It works part of only appear in the selected row. But with it came another problem, if I give tap and hold on another row missed the previous selected image – Felipe Xavier Dec 09 '15 at 11:06
  • So you need to store array of IndexPath, and check, if indexPath in `cellForRowAtIndexPath` match with one of them, you show, otherwise, you hide them. :) – Nghia Luong Dec 09 '15 at 11:10
  • how i use array in cellForRowAtIndexPath? – Felipe Xavier Dec 09 '15 at 11:18
  • You could use `NSMutableArray`, in this link: http://stackoverflow.com/questions/3480473/how-to-add-object-to-nsmutablearray – Nghia Luong Dec 09 '15 at 11:24