I've managed to use Swift's custom cells, so I'll recap the basics here for self-understanding.
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.
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".
It is to create a xib file of the UITableViewCell to be created and use it.
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.
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() {
super.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