我有一个带有可拖动行的UITableView,我可以添加/删除项目.数据源是NSMutableArray.
现在,如果我使用"添加新功能"移动该行,则应用程序崩溃,因为dataSource较小,因为尚未添加此行.
所以我修改了这段代码:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row >= [dataList count]) return NO;
        return YES;
    }
而现在我再也无法移动它了.但是我仍然可以在这样的行之后移动其他行,因此代码崩溃了.
我怎么解决这个问题?有没有办法禁用拖动"到"特定行而不仅仅是从?
谢谢
Mat*_*ick 17
之前的答案和文档(见其他答案中提到的这个和这个)是有帮助的,但不完整.我需要:
不用多说,这里有一些
- (NSIndexPath *)tableView:(UITableView *)tableView
    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
                         toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    return proposedDestinationIndexPath;
}
- (NSIndexPath *)tableView:(UITableView *)tableView
    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
                         toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    return sourceIndexPath;
}
- (NSIndexPath *)tableView:(UITableView *)tableView
    targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
                         toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (... some condition ...) {
        return sourceIndexPath;
    }
    return proposedDestinationIndexPath;
}
如何进行最后一行修复:
- (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;
    }
}
小智 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]
}