How to use UITableView in Swift
Tags: swift, mobile, beginners
Published: 2023-11-01 18:14:19

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ae5w9qj4brlugcdvm04q.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6c0uzpospntltitq8xu.png)

**Steps**
**1.Create an `Outlet` of the UITableView**

```swift
@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"]
```
Make sure to set `Prototype Cells` = 1 
select tabieView > attributes inspector > prototype cells
![prototype cell](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8um1hvsccn5miyiobeol.png)

**2.Connect the `DataSource` and `Delegate` of the tableView with the ViewController**
```swift
    override func viewDidLoad() {
        super.viewDidLoad()      
        itemsTableView.dataSource = self
        itemsTableView.delegate = self
    }
```

**3.Assign a `identifier` to the cell**
select prototype cells > attributes inspector > identifier, `defaultCell`
![identifier](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdxc6ah9fqmy4wopupos.png)

**4.Add protocols: `UITableViewDataSource` and `UITableViewDelegate`**
```swift
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {...}
```

**5.Implement the required and optional methods of the tableView**
```swift
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
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0exlhzonge8tq68i0aa5.png)

**6.Set `Header` for the section**
```swift
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  return "Items"
}
```

**7.Delete a row**
_Option 1(default)_
```swift
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)
}
```
![option1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6ndnedr1nkm3jrzagis.png)

_Option 2(Custom Trailing Swipe Actions)_
```swift
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])
}
```
![option2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7xfj5utc46y1e2afnif.png)

**Custom Leading Swipe Actions**
```swift
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])
}
```
![custom leading swipe](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ul2nu5wllfym48dhml1i.png)