TableViewに表示された画像をクリックして
別画面に遷移する処理を実装しようとしております。
Breakpointで確認したところif selectedImage != nilで
処理が止まっており、移動することができません。
色々と調べましたが詰まってしまいました。
何かアドバイス等いただけませんでしょうか。
回答、よろしくお願いします。
var friends:[Friend] = [Friend]() override func viewDidLoad() { super.viewDidLoad() self.setupFriends() self.tableView.delegate = self self.tableView.detaSource = self } //参考元:http://qiita.com/BigSea/items/9aa35b95e5d4d1dc8a52 func setupFriends() { var f1 = Friend(itemName: "Google1", imageUrl: NSURL(string: "https://storage.googleapis.com/gweb-uniblog-publish-prod/static/blog/images/google-200x200.7714256da16f.png")) var f2 = Friend(itemName: "Google2", imageUrl: NSURL(string: "https://pbs.twimg.com/profile_images/809064074998710272/KJvmreRz_400x400.jpg")) var f3 = Friend(itemName: "Google3", imageUrl: NSURL(string: "https://lh3.googleusercontent.com/nYhPnY2I-e9rpqnid9u9aAODz4C04OycEGxqHG5vxFnA35OGmLMrrUmhM9eaHKJ7liB-=w300")) var f4 = Friend(itemName: "Google4", imageUrl: NSURL(string: "https://lh3.googleusercontent.com/7clxDYpmA-L1XXJP7wcRZMWV71MwDtZhubp1cF8Ss4cVjHFsqisncNP5vavacmMPhds=w300")) friends.append(f1) friends.append(f2) friends.append(f3) friends.append(f4) } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell { let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as CustomCell cell.setCell(friends[indexPath.row]) return cell }
//CustomCellクラス func setCell(friend :Friend) { self.itemName.text = friend.itemName do { let imageData :NSData = try NSData(contentsOfURL: friend.imageUrl!,options: NSDataReadingOptions.DataReadingMappedIfSafe) self.siteImage.image = UIImage(data:imageData) } catch { print("Error: can't create image.") } }
// 参考元:https://iphone-app-tec.com/ios/tableview-cell.html func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) { // [indexPath.row] から画像名を探し、UImage を設定 selectedImage = UIImage(named:"\(friends[indexPath.row])") if selectedImage != nil { // SubViewController へ遷移するために Segue を呼び出す performSegue(withIdentifier: "toDetailsController",sender: nil) } } // Segue 準備 override func prepare(for segue: UIStoryboardSegue, sender: Any!) { if (segue.identifier == "toSubViewController") { let subVC: SubViewController = (segue.destination as? SubViewController)! // SubViewController のselectedImgに選択された画像を設定する subVC.selectedImg = selectedImage } }
:参考元
https://iphone-app-tec.com/ios/tableview-cell.html
http://qiita.com/BigSea/items/9aa35b95e5d4d1dc8a52

回答2件
あなたの回答
tips
プレビュー