0

我在我的应用程序的列表(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”中图像也显示

在此处输入图像描述

4

2 回答 2

0

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

于 2015-12-09T10:55:48.740 回答
0

只需存储indexPath为全局,例如_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;
}

希望这会有所帮助。

于 2015-12-09T10:58:13.960 回答