2017-08-29 113 views
1

除了如何處理UIDocuments外,關於拖放的一切都很清楚。iOS 11拖放UIDocument

這是我(不工作)實現...

對於拖動:

func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { 

    let doc = DataManager.shared.storage[indexPath.row] // the UIDocument to drag 
    let itemProv = NSItemProvider() 
    itemProv.registerFileRepresentation(forTypeIdentifier: "com.mybundle.myapp.myextension", 
             fileOptions: [.openInPlace], 
             visibility: .all) { completionHandler in 

     completionHandler(doc.fileURL, true, nil) 
     return nil 
    } 

    // DragItem 
    let item = UIDragItem(itemProvider: itemProv) 

    return [item] 
} 

對於降:

func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) { 

    for item in coordinator.session.items { 
     item.itemProvider.loadFileRepresentation(forTypeIdentifier: "com.mybundle.myapp.myextension", completionHandler: { (url, error) in 

      if let docUrlOk = url { 
       debugPrint(urlOk.absoluteString) 
       DataManager.shared.load(docUrlOk) 
      } 
     }) 
    } 
} 

而且更新方法:

func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal { 

    if tableView.hasActiveDrag { 
     if session.items.count > 1 { 
      return UITableViewDropProposal(operation: .cancel) 
     } else { 
      return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath) 
     } 
    } else { 
     return UITableViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath) 
    } 
} 

非常感謝你

回答

0

如果您在iPhone中運行代碼,則需要設置dragItem.enable = YES

+0

什麼是dragItem?這是一個UIDragItem實例。如果是,那麼沒有.enable屬性... – Enlil

+0

對不起,我的意思是UIDragInteraction – Kingandyoga

+0

的ebable屬性已啓用:tableView.dragDelegate = self - tableView.dropDelegate = self - tableView.dragInteractionEnabled = true – Enlil