微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS – 在“UITableView”中允许默认的行移动,而不需要编辑模式

我想允许在UITableView中的认行移动,而不会处于编辑模式,并且不会影响UITableView的认行为.

上图显示编辑模式下的单元格,启用移动.

我试图简单地运行(在UIView * subview在cell.subviews)(当我的UITableView处于编辑模式),但按钮没有出现:

<UITableViewCellScrollView: 0x8cabd80; frame = (0 0; 320 44); autoresize = W+H; gestureRecognizers = <NSArray: 0x8c9ba20>; layer = <CALayer: 0x8ca14b0>; contentOffset: {0,0}>

如何允许/添加移动“按钮”而不启用我的UITableView中的编辑模式?

创建和添加具有认运动功能的UIButton也是一种选择.

解决方法

我实际上为我的一个应用程序做了类似的事情.它使用委托方法进行表格编辑,并使用户“欺骗”一点. 100%内置苹果功能.

1 – 将表设置为编辑(我在viewWillAppear中执行)

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView setEditing:YES];
}

2 – 隐藏认附件图标:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
        editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
        //remove any editing accessories (AKA) delete button 
        return UITableViewCellAccessoryNone;
       }

3 – 保持编辑模式,将所有内容移动到右侧(在单元格中)

-(BOOL)tableView:(UITableView *)tableView shouldindentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
return NO;
}

4 – 在这一点上,您应该可以拖动单元格,而不是看起来像编辑模式.在这里我们欺骗用户.创建自己的“移动”图标(认情况下三行,您想要的任何图标),并将imageView正确放置在单元格上.

5 – 最后,实现委托方法来实际重新排列你的底层数据源.

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

    //Get original id
    NSMutableArray *originalArray = [self.model.items objectAtIndex:sourceIndexPath.section];
    Item * original = [originalArray objectAtIndex:sourceIndexPath.row];

    //Get destination id
    NSMutableArray *destinationArray = [self.model.items objectAtIndex:destinationIndexPath.section];
    Item * destination = [destinationArray objectAtIndex:destinationIndexPath.row];

    CGPoint temp = CGPointMake([original.section intValue],[original.row intValue]);

    original.row = destination.row;
    original.section = destination.section;

    destination.section = @(temp.x);
    destination.row = @(temp.y);

    //Put destination value in original array
    [originalArray replaceObjectAtIndex:sourceIndexPath.row withObject:destination];

    //put original value in destination array
    [destinationArray replaceObjectAtIndex:destinationIndexPath.row withObject:original];

    //reload tableview smoothly to reflect changes
    dispatch_async(dispatch_get_main_queue(),^{
        [UIView transitionWithView:tableView duration:duration options:UIViewAnimationoptionTransitionCrossdissolve animations:^{
            [tableView reloadData];
        } completion:NULL];
    });
 }

原文地址:https://www.jb51.cc/iOS/329336.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐