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.
In this explanation, tapping "Google" in TabelViewCell will take you to the "Google" page.


This time, I chose "Cell".




OutisideTableViewController.swift
 //This time I will link to Google
    var outsideArray = ["Google"]
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
    }
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)
        }
    }
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