The basic basis of Swift custom cells

The basic basis of Swift custom cells

I've managed to use Swift's custom cells, so I'll recap the basics here for self-understanding.

What is a custom cell in the first place?

The "UITableViewCell class" displays the cell part that displays each UITableView.

For XCode, a simple UITableViewCell design and configuration is prepared from the beginning.

However, there are many situations where you want to create an original composition, such as when the content you want to express is different in the composition that was originally prepared. In such a case, you can create a custom cell, and you can use the UITableViewCell class to customize the appearance and contents as you like.

Xib is recommended for custom cells

It is possible to place and use a custom UITableViewCell directly on the UITableView of Storyboard.

However, it cannot be diverted on another screen, and if you want to use the same cell, you will have to create a custom UITableViewCell each time. In order to follow the DRY principle, it is more efficient to be able to create cells in a form that can be diverted.

The method of creating a "custom cell that can be created in a form that can be diverted" is "to xib".

What is xib conversion?

It is to create a xib file of the UITableViewCell to be created and use it.

xib UITableViewCell method

func awakeFromNib() A method called immediately after the Cell is loaded, such as in a Storyboard or nib file

func setSelected() Processing method called when a cell is tapped and the selected state/normal state is switched.

Key points when making the height of a custom cell variable

By default, the cell height is fixed. Even if the height is specified by the method of Tableview, all cells are specified with the same fixed value. Therefore, you can change the height of the cell to suit the contents of the cell by using the following method!

-Set AutoLayout for all vertical axes in the layout in UITableView (Set the upper and lower Auto Layout without omission of all textviews and bottoms between the top and bottom of the cell. Do not set the height for the parts you want to fix and the height for the parts you want to make variable. )

-Set the assumed value in TableView (It is not essential. It seems that cell processing will be faster if you know the expected height in advance)

override func viewDidLoad() {
      tableView.estimatedRowHeight = 50
      tableView.rowHeight = UITableViewAutomaticDimension

・ Do not specify height in table view You can specify the height with the following method, but it will be a fixed value, so do not use this method

//Do not set this!!
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 10


・ Let's create a custom cell by converting it to xib -When converted to xib, there are two methods from the beginning, so use them as needed. ・ If you want to make the height variable, set it for variable height.

Recommended Posts

The basic basis of Swift custom cells
The basic basis of Swift dialogs
The basic basis of Swift's Delegate
Understand the basic mechanism of log4j2.xml
[Swift] Set the Style of TableViewCell to something other than Custom
The story of encountering Spring custom annotation
Basic basis of Android asynchronous processing "AsyncTask"
[Swift] Termination of the program by assertion
[Swift] Change the color of SCN Node
[Swift] How to get the number of elements in an array (super basic)
[Swift] Termination of the program by the fatalError function
[Swift] Get the number of steps with CMP edometer
[Swift] This is the solution! Illustration of Delegate implementation
Find the approximate value of log (1 + x) in Swift
[Swift] How to get the document ID of Firebase
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)
The world of clara-rules (5)
The idea of jQuery
[Swift 5] Recognize ON/OFF of Switch in custom cell for each cell
Review the basic knowledge of ruby that is often forgotten
Now, I understand the coordinate transformation method of UIView (Swift)
[Swift] I tried to implement the function of the vending machine
[Swift] Lightly introduce the logic of the app that passed the selection
I tried to summarize the basic grammar of Ruby briefly
[Swift] Get the timing when the value of textField is changed
Aiming for a basic understanding of the flow of recursive processing