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?".
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
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.
Each (row) in UITableView is made up of UITableViewCell class. The UITableViewCell will be displayed as a slur on the UITableView.
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.
-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.
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
When I often enter the code and start it, "tableview does not work" or "data is not displayed" may occur, but I usually forget to set the delegate. This is because each delegate works like this.
Protocol: A function that defines properties and methods implemented by classes and structures. In UIViewController, it is necessary to implement the protocol and always implement the properties and methods defined in the protocol.
Basically, only the ones to be used will be explained. There are others, so if you are interested, this article will be very helpful. UITableView Delegate Method Summary
Tableview works at least if there are two methods "numberOfRowsInSection" and "..cellForRowAt .."!
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