tableview的右拉滑动手势的时候显示出更多的按钮+demo

本文介绍了如何在UITableView中实现右滑显示多个自定义按钮,包括删除、置顶和更多等操作。通过UITableViewDelegate的tableView:editActionsForRowAtIndexPath:方法创建UITableViewRowAction对象并设置样式、文字和背景色,然后在Block中处理点击事件。
摘要由CSDN通过智能技术生成

       我们在使用tableview的时候,在滑动一些联系人的某一行的时候,会出现删除、置顶、更多等等的按钮,系统已经写好了,只需要一个代理方法和一个类就搞定了

       UITableViewDelegate协议多了一个方法,返回值是数组的tableView:editActionsForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableViewRowAction

        在UITableViewRowAction类,我们可以设置按钮的样式、显示的文字、背景色、和按钮的事件(事件在Block中实现)

        在代理方法中,我们可以创建多个按钮放到数组中返回,最先放入数组的按钮显示在最右侧,最后放入的显示在最左侧

     




    #pragma mark 设置某行上显示的内容

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

{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    
    
    
    cell.textLabel.text = _MyDataArray[indexPath.row];
        
    return cell;
    
}




#pragma mark 设置可以进行编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    return YES;
    
}

#pragma mark 设置编辑的样式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    return UITableViewCellEditingStyleDelete;
    
}

#pragma mark 设置处理编辑情况

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        // 1. 更新数据
        
        [_MyDataArray removeObjectAtIndex:indexPath.row];
        
        
        
        // 2. 更新UI
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }
    
    
    
}


#pragma mark 设置可以移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    return YES;
    
}

#pragma mark 处理移动的情况

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{
    
    // 1. 更新数据
    
    NSString *title = _MyDataArray[sourceIndexPath.row];
    
    [_MyDataArray removeObject:title];
    
    [_MyDataArray insertObject:title atIndex:destinationIndexPath.row];
    
    
    
    // 2. 更新UI
    
    [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
    
}







#pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    // 添加一个删除按钮
    
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        NSLog(@"点击了删除");
        
        // 1. 更新数据
        
        [_MyDataArray removeObjectAtIndex:indexPath.row];
        
        // 2. 更新UI
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }];
    
        // 删除一个置顶按钮
    
        UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    
            NSLog(@"点击了置顶");
    
            // 1. 更新数据
    
            [_MyDataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
    
            // 2. 更新UI
    
            NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
    
            [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
    
        }];
    
        topRowAction.backgroundColor = [UIColor blueColor];
    
    
    
    // 添加一个更多按钮
    
    UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        
        NSLog(@"点击了更多");
        
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        
    }];
    
    moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    
    
    // 将设置好的按钮放到数组中返回
    
     return @[deleteRowAction, topRowAction, moreRowAction];
    
}
 

                      demo  :http://download.csdn.net/detail/zhanniuniu/9367191

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 iOS 开发,嵌套在 `UIScrollView` 的 `UITableView` 在滑动时可能会与 `UIScrollView` 的滑动手势产生冲突,导致无法正常滑动。这个问题可以通过以下两种方式解决: 1. 禁用 `UIScrollView` 的滑动手势 可以通过设置 `UIScrollView` 的 `panGestureRecognizer` 的 `enabled` 属性为 `NO` 来禁用滑动手势,这样就不会与 `UITableView` 的滑动手势产生冲突了。 ```objc scrollView.panGestureRecognizer.enabled = NO; ``` 2. 实现 `UIGestureRecognizerDelegate` 协议的方法 在 `UIViewController` 实现 `UIGestureRecognizerDelegate` 协议的 `gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:` 方法,可以控制两个手势是否允许同时识别。在这个方法,可以判断当前的手势是否为 `UIScrollView` 的滑动手势,如果是,则允许与 `UITableView` 的滑动手势同时识别,否则不允许。 ```objc - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([gestureRecognizer.view isKindOfClass:[UIScrollView class]] && [otherGestureRecognizer.view isKindOfClass:[UITableView class]]) { return YES; } return NO; } ``` 需要注意的是,在实现这个方法时,要将 `UIScrollView` 的 `delegate` 设置为当前的 `UIViewController`,否则这个方法不会被调用。 ```objc scrollView.delegate = self; ``` 以上两种方式都可以解决嵌套在 `UIScrollView` 的 `UITableView` 滑动手势冲突的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值