The basics of Swift's TableView

The basics of Swift's TableView

I will summarize the basics of TableView, assuming that a friend who is a non-engineer and is studying Swift recently asks "What is TableView?".

What is TableView?

Officially the UITableView class. UITableView is a function to display items (Cells) row by row.

Often, in Todo apps and news apps, I think that there is a function that scrolls with information with different contents (photos, words, messages) lined up from top to bottom with the same UI. TableView is used as the function to display it.

The easiest example to imagine: SNS such as Twitter and Instagram, news apps such as 〇〇 News

* By the way, UITableView only scrolls vertically. Side-scrolling is another class

The UITableView class only scrolls vertically.

Therefore, people who often see the list of distributors by scrolling vertically and horizontally scrolling in one screen, which is often seen in live distribution applications, often use Collection View.

About Cell of UITableView

Each (row) in UITableView is made up of UITableViewCell class. The UITableViewCell will be displayed as a slur on the UITableView.

UITableViewCell location: about section and row

In UITableView, I think there are scenes that are often separated and displayed. For example, on the setting screen, you will often see UIs that have titles such as "Terms-related", "About services", and "Others", and there are Cell options in them.

Section and row are specified in the configuration.

section In a word, it is a "group of rows".

It is about the big group of ["Terms related", "About services", "Others"] mentioned in the previous example. There will be multiple rows (cells) in one section.

row In a word, it is "1 row (= 1 cell)". One cell is arranged for each row in the minimum unit.

Example

-It is the composition of section: 1, cell: n that is displayed in a row without separation on SNS etc. -If you create each cell: 5 in section: 3, you will have a total of 15 cells.

UITableView delegate protocol

Set the following delegate in UIViewController.

delegate -UITableViewDelegate protocol Properties that set delegates to handle events that occur in UITableView

dataSource · UITableViewDataSource protocol Properties for supplying the data to be displayed in UITableView

Delegate method

numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1 //Int
    }

As the word numberOfRowsInSection says, it specifies the number of rows (= cells) in the section.

numberOfSections

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

As the word numberOfSections literally sets the number of sections.

cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) //Change withIdentifier etc. for custom cells
return cell
}

As literally cellForRowAt, specify a cell for each row. Specifically, create a cell object and set it as the return value.

Since it can be set for each row, the cell object that is the return value can be dynamically changed by if / switch, etc., based on row and sction.

didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

Literally, it describes the processing when tableview is tapped. Since the tapped section and row can be obtained from IndexPath, the processing content can be dynamically determined based on that.

willDeselectRowAt

func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath? {

return indexPath
}

Specifies which cells to deselect when cells are untapped.

didDeselectRowAt

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

}

Called when cell is deselected

Recommended Posts

The basics of Swift's TableView
Docker monitoring-explaining the basics of basics-
Understand the basics of docker
About the basics of Android development
The basics of SpringBoot + MyBatis + MySQL
[Challenge CircleCI from 0] Learn the basics of CircleCI
Understand the basics of Android Audio Record
Now, I've summarized the basics of RecyclerView
Basics of Ruby
[day: 5] I summarized the basics of Java
Looking back on the basics of Java
What is JSP? ~ Let's know the basics of JSP !! ~
[Ruby] Summary of class definitions. Master the basics.
I understood the very basics of character input
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
Basics of try-with-resources statement
The world of clara-rules (5)
The idea of quicksort
The idea of jQuery
It was the Title of NavigationItem instead of the header of TableView
[For beginners] DI ~ The basics of DI and DI in Spring ~
[For beginners] Quickly understand the basics of Java 8 Lambda
I summarized the types and basics of Java exceptions
[Ruby] Class nesting, inheritance, and the basics of self
About the handling of Null
[Rails] Introduction of devise Basics
[GCD] Basics of DispatchQueue class
Basics of character operation (java)
I tried to summarize the basics of kotlin and java
About the description of Docker-compose.yml
The play of instantiating java.lang.Void
A story packed with the basics of Spring Boot (solved)
Explanation of the FizzBuzz problem
Summary of Java language basics
Median of the three values
The illusion of object orientation
[Summary of technical books] Summary of reading "Learn Docker from the basics"
Switch the version of bundler
The basics of the process of making a call with an Android app
About the behavior of ruby Hash # ==
Basics of conditional branching and return
[Java] Delete the elements of List
Continuation: The parable of OOP (omitted)
Qualify only part of the text
Understand the basic mechanism of log4j2.xml
Basics of sending Gmail in Ruby
'% 02d' What is the percentage of% 2?
[Rails] Check the contents of the object
Replace the contents of the Jar file
[Java version] The story of serialization
[Swift] Change the textColor of UIDatePicker
[Ruby] See the essence of ArgumentError
Note on the path of request.getRequestDispatcher
The basic basis of Swift dialogs
I read the source of Integer
This and that of the JDK
Check the migration status of rails