[Swift] Table View Cell, how to jump to an external URL

Introduction

I wanted to use TableViewCell and put a link to my homepage in the app, but I couldn't find it right away, so I'll write it as a reminder.

Finished product

In this explanation, tapping "Google" in TabelViewCell will take you to the "Google" page.

Set Table View on Story board

スクリーンショット 2020-10-30 6.24.13.png

Give the Identifier a name

This time, I chose "Cell". スクリーンショット 2020-10-30 6.26.00.png

Create a TableViewControllre.swif file

スクリーンショット 2020-10-30 6.29.12.png

スクリーンショット 2020-10-30 6.29.37.png

Connect TableView and TableViewController.swift

スクリーンショット 2020-10-30 6.32.16.png

Prepare an array to put in the cell

OutisideTableViewController.swift


 //This time I will link to Google
    var outsideArray = ["Google"]

Put a value in a cell

OutisideTableViewController.swift


 override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return outsideArray.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //Put the name set in Identifier in sithIdentifier
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        cell.textLabel?.text = outsideArray[indexPath.row]

        return cell
    }

Process to open an external browser

OutisideTableViewController.swift


//Processing when a cell is tapped
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        //Erases the selected color when tapped
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
        
        //Open the URL in an external browser
        let url = NSURL(string: "https://www.google.com/?hl=ja")
        if UIApplication.shared.canOpenURL(url! as URL) {
            UIApplication.shared.open(url! as URL, options: [:], completionHandler: nil)
        }
    }

Full code

OutisideTableViewController.swift


import UIKit

class OutsideTableViewController: UITableViewController {
    
   //This time I will link to Google
    var outsideArray = ["Google"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return outsideArray.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //Put the name set in Identifier in sithIdentifier
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        cell.textLabel?.text = outsideArray[indexPath.row]

        return cell
    }
    
    //Processing when a cell is tapped
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        //Erases the selected color when tapped
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
        
        //Open the URL in an external browser
        let url = NSURL(string: "https://www.google.com/?hl=ja")
        if UIApplication.shared.canOpenURL(url! as URL) {
            UIApplication.shared.open(url! as URL, options: [:], completionHandler: nil)
        }
    }
}

Reference site http://somen.site/2018/09/24/%E3%82%A2%E3%83%97%E3%83%AA%E3%81%8B%E3%82%89%E5%A4%96%E9%83%A8%E3%83%96%E3%83%A9%E3%82%A6%E3%82%B6%E3%81%A7url%E3%82%92%E9%96%8B%E3%81%8F-swift4-1/ https://pg-happy.jp/swift-tableview-tableviewcell.html

Recommended Posts

[Swift] Table View Cell, how to jump to an external URL
[swift5] How to transition from the app to an external site by specifying the URL
How to insert an external library
How to write an external reference key in FactoryBot
[Rails] How to display an image in the view
How to create an application
[Swift] How to use UserDefaults
How to use Swift UIScrollView
How to handle an instance
[Swift5] How to get an array and the complement of arrays
Correspondence when HTTP status code 302 occurs when connecting to an external URL
How to add columns to a table
How to "hollow" View on Android
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
[Swift] How to send a notification
[Swift] How to replace multiple strings
[Swift] Summary of how to remove elements from an array (personal note)
[Swift] How to set an image in the background without using UIImageView.
How to crop an image with libGDX
How to use binding.pry for view files
How to blur an image (super easy)
How to publish an application on Heroku
[Swift] How to fix Label in UIPickerView
[Swift] How to use Tab Bar Controller
How to call Swift 5.3 code from Objective-C
[Swift] How to implement the countdown function
How to define an inner class bean
[Swift5] How to implement standby screen using'PKHUD'
[Swift5] How to create a splash screen
[Swift] Template (like) when creating Table View
[Swift5] How to implement animation using "lottie-ios"
Ruby: CSV :: How to use Table Note
How to overwrite Firebase data in Swift
[Swift] How to use one option alert
How to write an RSpec controller test
[Swift] How to get the number of elements in an array (super basic)
[Swift] How to generate an ID to uniquely identify a certain thing (using UUID)