UITableViewで写真のアルバム名を表示して、アルバムを選択できるようにしたいのですが、UITableViewのcellが選択できません。
http://qiita.com/__mick/items/15426792558a94951ac0
http://qiita.com/Masaaki/items/8779c96d18299b076685
などを参考に調べてみたのですが解決しませんでした。
cellが選択ができるようにするにはどうしたらいいのでしょうか。回答よろしくお願いします。
swift3 Xcode8.3.3
import UIKit import Photos extension PHPhotoLibrary { //ユーザーに許可を促す. class func Authorization(){ PHPhotoLibrary.requestAuthorization { (status) -> Void in switch(status){ case .authorized: print("Authorized") case .denied: print("Denied") case .notDetermined: print("NotDetermined") case .restricted: print("Restricted") } } } } class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { // アルバム. var myAlbum: [String] = [] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // フォトアプリの中にあるアルバムを検索する. let list = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.album, subtype: PHAssetCollectionSubtype.any, options: nil) // リストの中にあるオブジェクトに対して1つずつ呼び出す. for i in 0 ..< list.count { let item = list.object(at: i) // アルバムのタイトル名をコレクションする. myAlbum.append(item.localizedTitle!) } // 結果表示用のTableViewを用意. let barHeight = UIApplication.shared.statusBarFrame.size.height let displayWidth = self.view.frame.width let displayHeight = self.view.frame.height // TableViewを生成. let myTableView: UITableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight)) myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") myTableView.dataSource = self self.view.addSubview(myTableView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - UITableViewDataSource // Cell数を返すメソッド. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return myAlbum.count } // Cellの初期化をするメソッド. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) // Cellに値を設定. cell.textLabel?.text = myAlbum[indexPath.row] return cell } //Cellがタップされデータ選択後に呼び出されるメソッド func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let selectData = tableView.cellForRow(at: indexPath as IndexPath)!.textLabel!.text print(selectData) } }
回答2件
あなたの回答
tips
プレビュー