DEV Community

Avelyn Hyunjeong Choi
Avelyn Hyunjeong Choi

Posted on • Updated on

How to use UITableView in Swift

Image description

Image description

Steps
1.Create an Outlet of the UITableView

@IBOutlet weak var itemsTableView: UITableView!

var items = ["Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 7","Item 8","Item 9","Item 10","Item 11","Item 12","Item 13","Item 14","Item 15","Item 16","Item 17","Item 18","Item 19","Item 20","Item 21","Item 22","Item 23","Item 24","Item 25","Item 26","Item 27","Item 28","Item 29","Item 30","Item 31","Item 32","Item 33","Item 34","Item 35","Item 36","Item 37","Item 38","Item 39","Item 40","Item 41","Item 42","Item 43","Item 44","Item 45","Item 46","Item 47","Item 48","Item 49","Item 50"]
Enter fullscreen mode Exit fullscreen mode

Make sure to set Prototype Cells = 1
select tabieView > attributes inspector > prototype cells
prototype cell

2.Connect the DataSource and Delegate of the tableView with the ViewController

    override func viewDidLoad() {
        super.viewDidLoad()      
        itemsTableView.dataSource = self
        itemsTableView.delegate = self
    }
Enter fullscreen mode Exit fullscreen mode

3.Assign a identifier to the cell
select prototype cells > attributes inspector > identifier, defaultCell
identifier

4.Add protocols: UITableViewDataSource and UITableViewDelegate

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {...}
Enter fullscreen mode Exit fullscreen mode

5.Implement the required and optional methods of the tableView

func numberOfSections(in tableView: UITableView) -> Int {
  return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return items.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let itemCell = tableView.dequeueReusableCell(withIdentifier: "defaultCell", for: indexPath)
  itemCell.textLabel?.text = items[indexPath.row]
  return itemCell
}
Enter fullscreen mode Exit fullscreen mode

Image description

6.Set Header for the section

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  return "Items"
}
Enter fullscreen mode Exit fullscreen mode

7.Delete a row
Option 1(default)

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  // remove the item from the array
  items.remove(at: indexPath.row)

  // remove the item from the tableView
  tableView.deleteRows(at: [indexPath], with: .automatic)
}
Enter fullscreen mode Exit fullscreen mode

option1

Option 2(Custom Trailing Swipe Actions)

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
  let deleteAction = UIContextualAction(style: .destructive, title:  "", handler: { (_, _, success:(Bool) -> Void) in

    self.items.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .automatic)
    success(true)
  })
  deleteAction.image = UIImage(systemName: "trash")
  deleteAction.backgroundColor = .systemRed

  let infoAction = UIContextualAction(style: .normal, title:  "", handler: { (_, _, success:(Bool) -> Void) in

    print(self.items[indexPath.row])
    success(true)
  })
  infoAction.image = UIImage(systemName: "info.circle")
  infoAction.backgroundColor = .systemBlue

  return UISwipeActionsConfiguration(actions: [deleteAction, infoAction])
}
Enter fullscreen mode Exit fullscreen mode

option2

Custom Leading Swipe Actions

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
  let infoAction = UIContextualAction(style: .normal, title:  "", handler: { (_, _, success:(Bool) -> Void) in

    print(self.items[indexPath.row])
    success(true)
  })
  infoAction.image = UIImage(systemName: "info.circle")
  infoAction.backgroundColor = .systemBlue

  return UISwipeActionsConfiguration(actions: [infoAction])
}
Enter fullscreen mode Exit fullscreen mode

custom leading swipe

Top comments (0)