[Swift] Set the Style of TableViewCell to something other than Custom

Introduction

When I set the Style of this TableViewCell to something other than Custom, I couldn't figure out how to access the parts inside the Cell and got stuck. This time I will write the explanation. It's easy once you know it.

How to change Style

If you select TableViewCell, there is a Style in the red frame, so you can change it from there. スクリーンショット 2020-12-28 16.05.36.png

Access to parts in Cell

Since cell is generated, we will write it in cellForRowAt.

Define a constant cell and use tableView.dequeueReusableCell (withIdentifier :, for :) to reuse the cell.

You will specify the Identifier and reuse the cell associated with it.

Then you will be able to access that cell,

Specify the part like cell.textLabel? .Text =" abc " and write the process.

In the case of this Style, the part name on the left is textLabel and the part name on the right is detailLabel.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
        cell.textLabel?.text = "abc"
        cell.detailTextLabel?.text = "def"
        
        return cell
    }

I set the numberOfRowsInSection to 4, so

As for the execution result, 4 Identidier cells specified by dequeueReusableCell are generated. スクリーンショット 2020-12-28 16.21.46.png

Recommended Posts

[Swift] Set the Style of TableViewCell to something other than Custom
The basic basis of Swift custom cells
Set the time of LocalDateTime to a specific time
Mandels to create an image of the Mandelbrot set
[Swift] How to get the document ID of Firebase
[Swift] I tried to implement the function of the vending machine
I want to use the sanitize method other than View.
How to set the IP address and host name of CentOS8
Fix the file name of war to the one set in Maven
[Swift UI] How to get the startup status of the application [iOS]
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
[Swift] Change the textColor of UIDatePicker
Set Rails routing other than id
The basic basis of Swift dialogs
How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK
[Swift] How to set an image in the background without using UIImageView.
[swift5] How to change the color of TabBar or the color of item of TabBar with code