iOS-允许在UITableView中默认行移动而无需编辑模式 [英] iOS - Allow default row movement in `UITableView` without editing mode

查看:336
本文介绍了iOS-允许在UITableView中默认行移动而无需编辑模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许 UITableView 中的默认行移动,而不使其处于编辑模式,并且不影响 UITableView <的默认行为。 / code>。

I want to allow the default row movement in a UITableView without it being in editing mode, and without compromising the default behaviour of the UITableView.

上面的图像在编辑模式下显示了一个单元格,并且启用了移动。

The image above displays a cell in editing mode, with movement enabled.

我尝试简单地运行 for(UIView * cell.subviews中的子视图)(而我的 UITableView 处于编辑模式时),但是按钮没有出现:

I tried simply running for (UIView *subview in cell.subviews) (while my UITableView was in editing mode), but the button didn't turn up:

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

如何在我的 UITableView

创建并添加 UIButton ,默认值为功能移动也是一种选择。

Creating and adding a UIButton with the default function for movement is also an option.

推荐答案

我实际上对其中一个做了类似的操作我的应用程序。它使用委托方法进行表编辑,并吸引用户。 100%内置的Apple功能。

I actually do something similar for one of my apps. It uses the delegate methods for table editing and a bit of 'tricking' the user. 100% built-in Apple functionality.

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添加到通常在单元格上的位置。

4 - At this point you should be able to drag the cells around without it looking like it is in editing mode. Here we trick the user. Create your own "move" icon (three lines in the default case, whatever icon you want in your case) and add the imageView right where it would normally go on the cell.

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];
    });
 }

这篇关于iOS-允许在UITableView中默认行移动而无需编辑模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆