UITableView:如何禁用将项目拖动到特定行?

ane*_*yzm 24 uitableview ios

我有一个带有可拖动行的UITableView,我可以添加/删除项目.数据源是NSMutableArray.

现在,如果我使用"添加新功能"移动该行,则应用程序崩溃,因为dataSource较小,因为尚未添加此行.

所以我修改了这段代码:

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row >= [dataList count]) return NO;
        return YES;
    }
Run Code Online (Sandbox Code Playgroud)

而现在我再也无法移动它了.但是我仍然可以在这样的行之后移动其他行,因此代码崩溃了.

我怎么解决这个问题?有没有办法禁用拖动"到"特定行而不仅仅是从?

谢谢

Gar*_*ies 39

这正是这个UITableViewDelegate方法

-tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:

是为了.它会适合你的目的吗? 这是文档.


Mat*_*ick 17

之前的答案和文档(见其他答案中提到的这个这个)是有帮助的,但不完整.我需要:

  • 例子
  • 如果建议的行动不合适,知道该怎么办

不用多说,这里有一些

例子

接受所有动作

- (NSIndexPath *)tableView:(UITableView *)tableView
    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
                         toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    return proposedDestinationIndexPath;
}
Run Code Online (Sandbox Code Playgroud)

拒绝所有移动,将行返回到其初始位置

- (NSIndexPath *)tableView:(UITableView *)tableView
    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
                         toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    return sourceIndexPath;
}
Run Code Online (Sandbox Code Playgroud)

拒绝一些移动,将任何被拒绝的行返回到其初始位置

- (NSIndexPath *)tableView:(UITableView *)tableView
    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
                         toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (... some condition ...) {
        return sourceIndexPath;
    }
    return proposedDestinationIndexPath;
}
Run Code Online (Sandbox Code Playgroud)


Web*_*ode 8

如何进行最后一行修复:

- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
       toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    // get number of objects
    NSUInteger numberOfObjects = [[[BNRItemStore sharedStore] allItems] count]; 

    if ( (proposedDestinationIndexPath.row+1==numberOfObjects) || (sourceIndexPath.row+1==numberOfObjects) ) {
        NSLog(@"HERE");
        return sourceIndexPath;
    }
    else{
         NSLog(@"count=%d %d", [[[BNRItemStore sharedStore] allItems] count], proposedDestinationIndexPath.row);
        return proposedDestinationIndexPath;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

以下是将拖放限制到第 1 部分的第 0 个索引的示例UICollectionView

func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {

    if session.localDragSession != nil {
        // Restricts dropping to 0th index
        if destinationIndexPath?.row == 0 {
           return UICollectionViewDropProposal(operation: .forbidden) 
        }

        if collectionView.hasActiveDrag {
            return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
        } else {
            return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
        }
    }        
}

func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
    // Prevents dragging item from 0th index
    if indexPath.row == 0 {
        return [UIDragItem]() // Prevents dragging item from 0th index
    }

    let item = self.yourArray[indexPath.row]
    let itemProvider = NSItemProvider(object: item)
    let dragItem = UIDragItem(itemProvider: itemProvider)
    dragItem.localObject = item
    return [dragItem]
}       
Run Code Online (Sandbox Code Playgroud)

  • `return [UIDragItem]()` 以防止拖动某些单元格正是我所寻找的!谢谢你!!!!! (3认同)