> For the complete documentation index, see [llms.txt](https://pakornpat.gitbook.io/ios-app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pakornpat.gitbook.io/ios-app/v2/drag-item.md).

# Drag item

## ทำให้ลากได้ก่อน

เริ่มจากการบอก TableView ก่อนว่าฉัน (TodoListViewController) เป็น dragDelegate และ enable ให้ drag ได้

```swift
override func viewDidLoad() {
    super.viewDidLoad()
    todo.add(item: TodoItem(title: "Download XCode", isDone: true))
    todo.add(item: TodoItem(title: "Buy milk"))
    todo.add(item: TodoItem(title: "Learning Swift", isDone: false))
    
    tableView?.dragDelegate = self
    tableView?.dragInteractionEnabled = true
}
```

จากนั้น add conform UITableViewDragDelegate

```swift
UITableViewDragDelegate
```

UITableViewDragDelegate จะบังคับ ให้สร้าง function ก็ให้เราใส่ตามนี้

```swift
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
    return [UIDragItem(itemProvider: NSItemProvider())]
}
```

เนื่องจากเราจะทำ drag and drop ของในแอพเราเท่านั้น เราจึงใช้ itemProvider แบบนี้ได้

จากนั้นให้เราลองรันแอพของเรา จะพบว่า เราสามารถลากได้แล้ว

![](https://144845262-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGk7Utff0rVWsqK4eYO%2F-LfO5CRmL0x_sCBCSnBW%2F-LfO68ELGrxlhxy7hRST%2F47.gif?alt=media\&token=beece23d-cb25-4701-afa4-a40a8af30304)
