• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何从表格单元格中删除 UIGestureRecognizer

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:39:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我只想允许滑动删除 UITableView 的第一个单元格。 这一点很简单,但是当用户尝试滑动任何其他单元格时,我想显示一个 UIAlert 。我再次通过在除单元 0 之外的每个单元上使用 UIGestureRecognizer 来完成这项工作。

我遇到的问题是,一旦删除了顶行,我希望允许删除新的顶行。 好像我需要删除我分配给单元格的 UIGestureRecognizer's,但我不知道该怎么做。

这是我的一些代码

 - (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
    BetCell *cell = [tableView dequeueReusableCellWithIdentifier"BetCell"];

    Bet *bet = [self.bets objectAtIndex[self.bets count]-indexPath.row-1)];
    cell.BFNeedLabel.text = bet.BFNeeded;

    if (indexPath.row != 0) {
    UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self actionselector(swipeDetected];
    swipeRecognizer.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight);
    [cell addGestureRecognizer:swipeRecognizer];
    }

    return cell;

}

-(void)swipeDetectedUIGestureRecognizer *)sender
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle"Info" message"You can only delete starting from the top cell" delegate:self cancelButtonTitle"OK" otherButtonTitles:nil];
    [alert show];
}

-(BOOL)tableViewUITableView *)tableView canEditRowAtIndexPathNSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return YES;
    } else  { 
        return NO;
    }
}

-(void)tableViewUITableView *)tableView commitEditingStyleUITableViewCellEditingStyle)editingStyle forRowAtIndexPathNSIndexPath *)indexPath
{
    // If the very first row
    if ((editingStyle == UITableViewCellEditingStyleDelete) && (indexPath.row == 0)) {
        Bet *betObj = [self.bets objectAtIndex[self.bets count]-indexPath.row-1)];
        //Delete from array
        [self.bets removeObjectAtIndex:([self.bets count]-indexPath.row-1)];
        //Delete the row
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        //Attempt to remove gesture recognizer from cell 0
        UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self actionselector(swipeDetected];
        swipeRecognizer.direction = (UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight);
        [[tableView cellForRowAtIndexPath:0]removeGestureRecognizer:swipeRecognizer];
    }
}



Best Answer-推荐答案


无需移除gestureRecognizer。在您的 swipeDetected 中找出您所在的单元格,并且仅在 indexPath.row != 0 时显示警报。

您的手势识别器将为您提供可以转换为表格 View 坐标空间的位置,该坐标空间可用于获取该单元格的 indexPath。

swipeDetected

CGPoint location = [sender locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
if ( indexPath.row != 0 {
// do alert
}

更新示例代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier"Test"];
    if ( !cell ) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier"test"] autorelease];
        UISwipeGestureRecognizer *gr = [[UISwipeGestureRecognizer alloc] initWithTarget:self actionselector(handleCellSwipe];
        gr.direction = UISwipeGestureRecognizerDirectionRight + UISwipeGestureRecognizerDirectionLeft;
        gr.delegate = self;
        [cell addGestureRecognizer:gr];
        [gr release];
    }
    cell.textLabel.text = [NSString stringWithFormat"Cell %d",indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( indexPath.row == 0 )
        return UITableViewCellEditingStyleDelete;
    else 
        return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row == 0;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // do stuff
}

- (void)handleCellSwipe:(UIGestureRecognizer *)gestureRecognizer
{
    if ( gestureRecognizer.state == UIGestureRecognizerStateRecognized ) {
        CGPoint location = [gestureRecognizer locationInView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
        if ( indexPath.row != 0 ) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cell Swipe"
                                                            message:@"Cell in row not equal to 0 swiped"
                                                           delegate:nil 
                                                  cancelButtonTitle:nil
                                                  otherButtonTitles:@"OK", nil];
            [alert show];
            [alert release];
        }
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint location = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    return indexPath.row != 0;
}

关于ios - 如何从表格单元格中删除 UIGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8359833/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap