[swift]Tabelviewの中にbuttonを入れ、配列のURLを呼び出す
解決したいこと
ここに解決したい内容を記載し
Tabelviewの中にbuttonを入れてそのbuttonをタップすると配列で指定したURLに飛ぶようにしたいのですが配列で順番にURLを呼び出すためにはどのようにすればよいですか
発生している問題・エラー
出ているエラーメッセージを入力
```
例)
NameError (uninitialized constant World)
または、問題・エラーが起きている画像をここにドラッグアンドドロップ
該当するソースコード
ソースコードを入力
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
}
}
自分で試したこと
ここに問題・エラーに対して試したことを記載してください。
0 likes