如何使用自定义按钮重新排序表格视图单元格,但不使用 ios 中的移动编辑图标

Posted

技术标签:

【中文标题】如何使用自定义按钮重新排序表格视图单元格,但不使用 ios 中的移动编辑图标【英文标题】:How to Reorder tableview cell with a custom button, but not with move edit icon in ios 【发布时间】:2016-08-01 10:55:23 【问题描述】:

我正在开发一个应用程序,该应用程序具有重新排序 tableview 单元格的新要求。在 tableview 单元格中,我应该只显示一个标签。当我选择整个单元格时,它必须被选中并且能够将单元格拖动到新位置。

我有一个UITableView,我正在使用以下代码来管理重新排序:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    return YES;


-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    return YES;


-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    return UITableViewCellEditingStyleNone;


- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath 
    return NO;


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

    NSString *stringToMove = resourceDetailsArray[sourceIndexPath.row];
    [resourceDetailsArray removeObjectAtIndex:sourceIndexPath.row];
    [resourceDetailsArray insertObject:stringToMove atIndex:destinationIndexPath.row];
    [orderListTableView reloadData];

谁能帮我一个更好的解决方案?

提前致谢。

【问题讨论】:

这段代码有什么问题? 当我选择单元格时,我想重新排序单元格。否则在这里我如何隐藏移动图标? 【参考方案1】:

那里有一个名为ObjcReorder 的库,翻译自SwiftReorder。

【讨论】:

【参考方案2】:

这些代码行对我帮助很大。它对我的要求就像一个魅力,即长按表格视图单元格时重新排序单元格。

- (IBAction)longPressGestureRecognized:(id)sender 

    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
    UIGestureRecognizerState state = longPress.state;

    CGPoint location = [longPress locationInView:orderListTableView];
    NSIndexPath *indexPath = [orderListTableView indexPathForRowAtPoint:location];

    static UIView       *snapshot = nil;        ///< A snapshot of the row user is moving.
    static NSIndexPath  *sourceIndexPath = nil; ///< Initial index path, where gesture begins.

    switch (state) 
        case UIGestureRecognizerStateBegan: 
            if (indexPath) 
                sourceIndexPath = indexPath;

                UITableViewCell *cell = [orderListTableView cellForRowAtIndexPath:indexPath];

                // Take a snapshot of the selected row using helper method.
                snapshot = [self customSnapshoFromView:cell];

                // Add the snapshot as subview, centered at cell's center...
                __block CGPoint center = cell.center;
                snapshot.center = center;
                snapshot.alpha = 0.0;
                [orderListTableView addSubview:snapshot];
                [UIView animateWithDuration:0.25 animations:^

                    // Offset for gesture location.
                    center.y = location.y;
                    snapshot.center = center;
                    snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
                    snapshot.alpha = 0.98;
                    cell.alpha = 0.0;
                    cell.hidden = YES;

                ];
            
            break;
        

        case UIGestureRecognizerStateChanged: 
            CGPoint center = snapshot.center;
            center.y = location.y;
            snapshot.center = center;

            // Is destination valid and is it different from source?
            if (indexPath && ![indexPath isEqual:sourceIndexPath]) 

                // ... update data source.
                [resourceDetailsArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];

                // ... move the rows.
                [orderListTableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];

                // ... and update source so it is in sync with UI changes.
                sourceIndexPath = indexPath;
            
            break;
        

        default: 
            // Clean up.
            UITableViewCell *cell = [orderListTableView cellForRowAtIndexPath:sourceIndexPath];
            cell.alpha = 0.0;

            [UIView animateWithDuration:0.25 animations:^

                snapshot.center = cell.center;
                snapshot.transform = CGAffineTransformIdentity;
                snapshot.alpha = 0.0;
                cell.alpha = 1.0;

             completion:^(BOOL finished) 

                cell.hidden = NO;
                sourceIndexPath = nil;
                [snapshot removeFromSuperview];
                snapshot = nil;

            ];

            break;
        
    



/** @brief Returns a customized snapshot of a given view. */
- (UIView *)customSnapshoFromView:(UIView *)inputView 

    // Make an image from the input view.
    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
    [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Create an image view.
    UIView *snapshot = [[UIImageView alloc] initWithImage:image];
    snapshot.layer.masksToBounds = NO;
    snapshot.layer.cornerRadius = 0.0;
    snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
    snapshot.layer.shadowRadius = 5.0;
    snapshot.layer.shadowOpacity = 0.4;

    return snapshot;

【讨论】:

以上是关于如何使用自定义按钮重新排序表格视图单元格,但不使用 ios 中的移动编辑图标的主要内容,如果未能解决你的问题,请参考以下文章

如何按字母顺序对表格视图单元格进行排序

使用自定义维度重新排序集合视图单元格时出现问题

在没有自定义单元格的情况下向 uitableview 显示详细信息披露按钮

从自定义单元格重新加载表格视图数据

在 ios 中展开和折叠表格视图单元格

如何为自定义表格视图单元格中的按钮单击获取不同的视图控制器