错误使用UILongPressGestureRecognizer在单元格中添加图像

时间:2015-12-09 09:49:16

标签: objective-c uitableview uilongpressgesturerecogni

我点击并在我的应用程序的列表(UITableView)中保存事件:

ViewDidLoad PlayerViewController.m:

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

PlayerViewController.m中的函数agendarPrograma:

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

问题:当我使用点击并按住他时,在我选择的行上添加图像,但也添加其他图像。我需要它添加我正在使用点按并保持的位置。添加行选择,但在列表中随机添加其他图像。

示例:我使用“Sorrindo pra vida”中的点击并保持隐藏的图像等于假,但在“Musicas Marianas”中图像显示也是

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以将多个选定的表设置为false,并通过委托tableview didSelectRow atIndexPath获取正确的单元格。

答案 1 :(得分:0)

只需将_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; } }

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

并且在: if([indexPath compare:_indexPath] == NSOrderedSame) { cell.imageAgendamento.hidden = NO; }else{ cell.imageAgendamento.hidden = YES; } 你实现这个:

.controller('DemoController', function($scope, $ionicHistory) {
    $scope.goBack() = function(){
          $ionicHistory.viewHistory().backView.go();
    }

})

希望这可以提供帮助。