🖥️
iOS App with Pop
UIKit Swift 5
UIKit Swift 5
  • iOS App development
  • Swift
    • Variable & Constant
    • Number & String
    • Operator
    • Array, Dictionary & Tuple
    • Enum
    • Optional
    • Function
    • Class & Struct
    • Branching
    • Loops
    • Error handler
    • Protocol
    • Extension
  • Create New Project
  • Introduction to Xcode
  • Scene-Based 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
  • What is weak?
  • Finish add item
  • Delete todo item
  • Edit todo item
  • Custom new layout
  • Adding new delegate
  • Refactor
  • Pushing edit view
  • Large navigation
  • Drag item
  • Drop item (in app)
  • Save data
  • Where to go from here?
Powered by GitBook
On this page

Drag item

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

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

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

UITableViewDragDelegate

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

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

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

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

PreviousLarge navigationNextDrop item (in app)

Last updated 2 years ago