> 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/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] {
    [UIDragItem(itemProvider: NSItemProvider())]
}
```

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

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

![](https://759097476-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FuqWEUpYllBlpWTYMEIgR%2Fuploads%2FYDki8q9HGPg5Dq9J2WDc%2FJun-28-2022%204-21-58%20PM.gif?alt=media\&token=105e33f2-c850-4e07-a8e4-563a3d600ebd)
