[Swift] Implement UITableView using xib

Introduction

I will write about how to implement a custom cell using .xib when using UITableView. You can use xib to implement the description in calss of a custom cell based on Storyboard.

What to do in this article

Change Label text in xib of UITableViewCell Simulator Screen Shot - iPhone 12 - 2021-01-11 at 02.00.41.png

Implementation

Add TableView

Add a TableView to fill the screen. スクリーンショット 2021-01-11 2.03.14.png

Create xib file

Select Cocoa Touch Class and スクリーンショット 2021-01-11 2.05.40.png

Select UITableViewCell, check Also Create XIB file and create a file スクリーンショット 2021-01-11 2.07.02.png

Add Label

Add Label to Cell and connect to Outlet スクリーンショット 2021-01-11 2.16.03.png

Create TableView

Outlet connect TableView and code and write general TableView code () in extension スクリーンショット 2021-01-11 2.16.51.png

What is different from the general TableView here is that Register is described.

ViewController.swift


tableView.dataSource = self
tableView.delegate = self
//Set xib as identifier in register
tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "TableViewCell")

The point is to use the identifier set here.

ViewController.swift


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
    cell.titleLabel?.text = "aaa"

    return cell
}

Try to build

You should now be able to change the text of the UITableViewCell using xib as at the beginning. If you find it helpful, I would be grateful if you could give me LGTM!

Recommended Posts

[Swift] Implement UITableView using xib
[Swift5] How to implement animation using "lottie-ios"
Implement ProgressBar using library
[Swift] Implement swipe processing
[Swift] switch statement using tuples
[Swift] Try using Collection View
[Swift] Share data using AppDelegate
Implement category function using ancestory
[Swift] Asynchronous processing using PromiseKit
[Swift] How to implement the Twitter login function using Firebase UI ①
[Swift] How to implement the Twitter login function using Firebase UI ②
Implement declarative retry processing using Spring Retry
Implement user management functionality using Devise
[Swift 5] Implement UI for review function
[Java] Try to implement using generics
Implement pagination using SpringData Pageable + Thymeleaf