skt
@skt (SK T)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

【Swift】画面遷移時の値・受け渡し方

【Swift】画面遷移時の値・受け渡し方

SwiftでViewControllerよりAPIで取得した画像をButtonでLikeなのかButなのかを押下しLikeが押された時だけLike押下時の画像を保存
TabuleViewのCellにviewcontrollerでLike押下時に保存した画像を画面遷移させたい。

発生している問題・エラー

viewcontrollerでLike押下時で保存した画像を画面遷移させたいのですが画面遷移はできるがCellに画像が反映されない

例)

 var selectimages = [UIImage]()

または、問題・エラーが起きている画像をここにドラッグアンドドロップ

該当するソースコード

遷移元
import UIKit
import  Alamofire
import SwiftyJSON
import SDWebImage
import Photos

class ViewController: UIViewController {


    @IBOutlet weak var serchTextField: UITextField!
    @IBOutlet weak var KensakuImage: UIImageView!

    var count = 0
    var selectimages = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = true

    }


    //検索キーワードの値を元に画像を引っ張ってくる
    //pixabay.com
    func getImages(keyword:String){

        //APIKEY 9848333-b62d95ad945f6aae7e4f88e49
        let url = "https://pixabay.com/api/?key=19848333-b62d95ad945f6aae7e4f88e49&q=\(keyword)"
        //alamofireを使ってhttpリクエストを投げる
        AF.request(url, method:.get, parameters: nil, encoding: JSONEncoding.default).responseJSON { (response) in

            switch response.result {
            case.success:
                let json:JSON = JSON(response.data as Any)
                var imageString = json["hits"][self.count]["webformatURL"].string

                if imageString == nil{
                    imageString = json["hits"][0]["webformatURL"].string
                    self.KensakuImage.sd_setImage(with: URL(string: imageString!), completed: nil)

                }else{

                    self.KensakuImage.sd_setImage(with: URL(string: imageString!), completed: nil)
                }
            case.failure(let error):
                print(error)
            }
        }
    }

    @IBAction func serchButton(_ sender: Any) {
        self.count = 0
        if serchTextField.text == ""{
            getImages(keyword: "funny")

        }else{

            getImages(keyword:serchTextField.text!)
        }

    }

    @IBAction func goodButton(_ sender: Any) {

        count = count + 1
        selectimages.append(contentsOf: selectimages )
        if count == 10 {
            count = 0

            //            10回押された時に遷移させる
            self.performSegue(withIdentifier: "nextVC", sender: nil)


        }

        if serchTextField.text == ""{
            getImages(keyword: "funny")


        }else{

            getImages(keyword:serchTextField.text!)
        }


    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "nextVC"{


            //            segue.destination = 遷移先
            //            遷移先 = GoodViewController
            let goodView = segue.destination as! GoodViewController
            //            GoodViewControllerの中のselectimages
            goodView.selectimages = selectimages
        }

    }

    @IBAction func badButton(_ sender: Any) {
        count = count + 1
        if serchTextField.text == ""{
            getImages(keyword: "funny")

        }else{

            getImages(keyword:serchTextField.text!)
        }
    }

}


該当するソースコード

遷移先

import UIKit

class GoodViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!


    var selectimages = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self



        // Do any additional setup after loading the view.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return selectimages.count
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 390
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        let imageView = cell.contentView.viewWithTag(1) as! UIImageView

        return cell

    }

    @IBAction func backButton(_ sender: Any) {

        dismiss(animated: true, completion: nil)

    }
}



Xcode 12.5
ios 14

自分で試したこと

var selectimages = UIImageの配列を作り
タップされた時に値が入るように
selectimages.append(contentsOf: selectimages )
追加されるようにGoodLike Buttonを押下時入るようにセットしたができません。

0

1Answer

selectimages.append(contentsOf: selectimages)

これは空の配列に空の配列を追加しているだけです。
現在表示している画像を追加する必要があるのではないでしょうか。

0Like

Your answer might help someone💌