🖥️
iOS App with Pop
Swift 4
Swift 4
  • iOS App development
  • Create New Project
  • Introduction to Xcode
  • App's life cycle
  • UIViewController
  • Storyboard
  • First Run
  • Display todo list
  • Basic Auto Layout
  • MVC
  • Model
  • Binding TableView
  • Binding TableViewCell
  • TableViewDelegate
  • Add navigationBar with + button
  • Add new item page
  • TextField and Switch
  • Binding action
  • Add mock item to todo list
  • Finish add item
  • Delete todo item
  • Edit todo item
  • Custom new layout
  • Adding new delegate
  • Refactor
  • Save data
  • Pushing edit view
  • Large navigation
  • Drag item
  • Drop item (in app)
  • Where to go from here?
Powered by GitBook
On this page

Drag item

PreviousLarge navigationNextDrop item (in app)

Last updated 6 years ago

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

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

override func viewDidLoad() {
    super.viewDidLoad()
    loadTodo()
    tableView?.dragDelegate = self
    tableView?.dragInteractionEnabled = true
}

จากนั้น add conform UITableViewDragDelegate

UITableViewDragDelegate

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

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

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

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