UITableView Pt 7/10: DRAG AND DROP TO MOVE CELL IN TABLE VIEW SWIFT TUTORIAL

[ad_1]
JOIN THIS COURSE FOR FREE:

UITableView and UITableViewController are used in almost all iOS apps. This is one of the most critical skills every iOS developer must master.

In this course, we will build a photos browser app using Table View and Table View Controller.

Join me today in this course, you’ll learn everything about Table View in these 10 lessons:
1. Introduction to UITableView and UITableViewController
2: Create Data Model Classes to Display On UITableView & UITableViewController
3: Create Basic UITableView and UITableViewController, UITableViewCell
4: Create Custom Table View Cell, Dynamic Table View Cell Height, Self-Sizing Cell
5. Create multiple sections in Table View
6. Delete rows from table view
7. Use drag and drop to move cells around table view
8. Create static table view
9. Use show segue and navigation controller to transition from master to detail view controller
10. Where to go from here.

JOIN THIS COURSE FOR FREE:

********************************
*** ABOUT CODE MASTERY ***
********************************
Code Mastery is hosted by Duc Tran, founder of Developers Academy.

This is his free-style no notes, no teleprompter presentation and live coding broadcast with you guys everyday.

To join Duc’s free courses, register for free at

*************************
*** MEET DUC TRAN ***
*************************

Duc Tran is founder of Developers Academy, one of the world’s leading iOS, Android and Web development trainers.

More than 2,000,000 developers have studied his video trainings; 100,000 developers see his posts each month. Each year, Duc has helped 20,000 plus developers graduate from his online courses or video series.

*************************************************
*** FREE TRAININGS IN IOS DEVELOPMENT ***
*************************************************
To subscribe and get free tutorials, courses and weekly content, visit me at:
Connect with Duc on facebook:
Tweet him:
Get daily inspiration:

/************************************************/
CODE IN THIS COURSE – CONTROLLERS GROUP
/************************************************/

// ** AppleProductsTableViewController.swift
class AppleProductsTableViewController: UITableViewController
{
lazy var productLines: [ProductLine] = {
return ProductLine.productLines()
}()

// MARK: – VC Lifecycle

override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = editButtonItem

// Make the row height dynamic
tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = UITableViewAutomaticDimension
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

tableView.reloadData()
}

// MARK: – UITableViewDataSource

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) – String? {
let productLine = productLines[section]
return productLine.name
}

override func numberOfSections(in tableView: UITableView) – Int {
return productLines.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) – Int {
let productLine = productLines[section]
return productLine.products.count // the number of products in the section
}

// indexPath: which section and which row
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) – UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: “Product Cell”, for: indexPath) as! ProductTableViewCell

let productLine = productLines[indexPath.section]
let product = productLine.products[indexPath.row]

cell.configureCellWith(product)

return cell
}

// MARK: – Edit Tableview

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
let productLine = productLines[indexPath.section]
productLine.products.remove(at: indexPath.row)
// tell the table view to update with new data source
// tableView.reloadData() Bad way!
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
}

// ** JOIN THE COURSE AND GET ALL THE SOURCE CODE:


Posted

in

by

Tags:

Comments

2 responses to “UITableView Pt 7/10: DRAG AND DROP TO MOVE CELL IN TABLE VIEW SWIFT TUTORIAL”

  1. Abdulmalek S. A. Shefat Avatar

    thanks for the tutorial,

    how can we achieve the shadow below the cell?

  2. Mansur Muaz EKİCİ Avatar

    Thank you Ducky for perfect Tutorial 🙂 I have a question….

    How to prevent cells to go to another section while dragging?

Leave a Reply

Your email address will not be published. Required fields are marked *