前提・実現したいこと
Tabelviewの中にbuttonを入れてそのbuttonをタップすると配列で指定したURLに飛ぶようにしたいのですが配列で順番にURLを呼び出すためにはどのようにすればよいですか
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
swift
ソースコード
import UIKit
import SafariServices
class tabelview: UIViewController ,
UITableViewDataSource, UITableViewDelegate{
@IBOutlet var table:UITableView! // section毎の画像配列 let imgArray: NSArray = [ "img1", "img2","img3", "img4","img5",] let label2Array: NSArray = [ "100000円","8/23/16:15", "8/23/16:47","8/23/17:10", "8/23/1715:","8/23/17:21", "8/23/17:33","8/23/17:41"] override func viewDidLoad() { super.viewDidLoad() } //Table Viewのセルの数を指定 func tableView(_ table: UITableView, numberOfRowsInSection section: Int) -> Int { return imgArray.count } //各セルの要素を設定する func tableView(_ table: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // tableCell の ID で UITableViewCell のインスタンスを生成 let cell = table.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) let img = UIImage(named: imgArray[indexPath.row] as! String) // Tag番号 1 で UIImageView インスタンスの生成 let imageView = cell.viewWithTag(1) as! UIImageView imageView.image = img // Tag番号 2 で UILabel インスタンスの生成 let label1 = cell.viewWithTag(2) as! UILabel label1.text = "No." + String(indexPath.row + 1) // Tag番号 3 で UILabel インスタンスの生成 let label2 = cell.viewWithTag(3) as! UILabel label2.text = String(describing: label2Array[indexPath.row]) let Button1 = cell.viewWithTag(4) as! UIButton Button1.addTarget(self, action: #selector(self.Button(_:)), for: UIControl.Event.touchUpInside) return cell } let abc = [ "https://www.youtube.com/","https://www.youtube.com/watch?v=RgMONk-LLPM", "https://www.youtube.com/","https://www.youtube.com/", "https://www.youtube.com/","https://www.youtube.com/", "https://www.youtube.com/","https://www.youtube.com/"] @objc func Button(_ sender: UIButton) { let url = URL(string: abc)! if UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url) } } // Cell の高さを120にする func tableView(_ table: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 600.0 }
}
試したこと
補足情報(FW/ツールのバージョンなど)
let abc=[...]
のところからURLを取り出したいのですが
let url = URL(string: abc)!
の部分をどのように改良すればよいですか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/10/24 11:51
2021/10/24 11:56
2021/10/25 05:38
2021/10/25 12:13 編集
2021/10/25 14:06
2021/10/25 14:17