DEV Community

AJITHKUMAR K
AJITHKUMAR K

Posted on • Edited on • Originally published at dev.to

TableView

TableView
A table view is a user interface element that presents a scrollable list of items. It is a key component of iOS applications and is used to display and manage a collection of data in a vertically scrollable format. Table views are commonly used to display lists of items, such as contacts, settings, or emails.

In iOS, table views are implemented using the UITableView class, which is a subclass of UIScrollView. The UITableView class provides a number of features for displaying and managing data, including support for custom cells, reordering, and variable row heights. Table views can be populated with data from a variety of sources, such as arrays or database queries, and can be customized to suit the needs of your application.

To use a table view in an iOS application, you will typically create a subclass of UITableViewController and implement the necessary delegate and data source methods to manage the data displayed in the table. You can also use a UITableView as a standalone view by adding it to your view hierarchy and setting a data source and delegate.

TableView Delegate:

  • We can create the custom headers and footers for the sections in the tableview.

  • We can specify the custom heights for rows, headers, and footers.

  • Provide height estimates for the rows, headers, and footers.

  • We can define the method which can handle the row selections.

TableView Delegate Methods

  • tableView(UITableView, willDisplay: UITableViewCell, forRowAt: IndexPath)
    The tableview notifies this delegate when it is about to draw a cell for a particular row.

  • tableView(UITableView, willSelectRowAt: IndexPath) -> IndexPath?
    The tableview notifies this delegate method when the specified row is about to be selected.

  • tableView(UITableView, didSelectRowAt: IndexPath)
    This delegate is notified when the specified row of the tableview is selected.

  • tableView(UITableView, willDeselectRowAt: IndexPath) -> IndexPath?
    This delegate is notified when the particular cell is about to be deselected.

  • tableView(UITableView, didDeselectRowAt: IndexPath)
    This delegate is notified when the particular row is deselected.

  • tableView(UITableView, heightForRowAt: IndexPath) -> CGFloat
    This delegate method returns the height for the row.

TableView DataSource:
To maintain the data to be displayed by the tableview, we need to maintain a DataSource object that implements the UITableViewDataSource protocol. The datasource object manages the tableview data.

  • It reports the number of rows and sections to be displayed in the tableview.

  • It allocates the reusable cells for each row in the tableview.

  • It provides the titles for headers and footers in the tableview sections.

To perform the above-mentioned tasks, there are some functions defined in the UITableviewDataSource protocol.

TableViewDatasource Methods

  • tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    This method returns the number of rows to be displayed in the section of the tableview.

  • tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    This method returns the number of sections to be displayed in the tableview.

  • tableView(UITableView, cellForRowAt: IndexPath) -> UITableViewCell
    This method returns the object of a UITableViewCell, which shows the actual content of a particular row in the tableview. This method inserts the cell for a particular row in the tableview.

  • tableView(UITableView, titleForHeaderInSection: Int) -> String?
    This method returns a string representing the title of the header in the section of the tableview.

  • tableView(UITableView, titleForFooterInSection: Int) -> String?
    This method returns a string representing the title of the footer in the section of the tableview.

  • numberOfSections(in: UITableView) -> Int
    This method returns the number of sections to be displayed in the tableview.

  • sectionIndexTitles(for: UITableView) -> [String]?
    It returns the array of the string containing the titles for the sections in the tableview.

Sample App

Image description

Image description

sample app in tableView with sections

Image description

Customize TableView cell

To customize a table view cell in Swift, you'll need to do the following:

  • Create a new subclass of UITableViewCell.

  • Add any desired subviews to the cell's content view.

  • Override the setSelected() method of the cell and use it to position and configure the subviews.

  • Register the cell subclass with the table view using register(_:forCellReuseIdentifier:)

  • Dequeue and configure cells in the tableView(_:cellForRowAt:) data source method.

Image description

SwipAndAction in TableViewRow

  • tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle

  • func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }

 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete{
            tableView.beginUpdates()
            ContactList[indexPath.section].value.remove(at: indexPath.row)
            tableView.deleteRows(at:[indexPath], with: .right)
            tableView.endUpdates()
        }
    }

Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)