TableView 在新控制器上显示不同的图像

TableView to display different images on new controller

我终于创建了一个 tableview,它将填充一定数量的选项供用户单击。理想情况下,我希望用户单击一行,这将根据用户的选择在第二个控制器上显示图像。例如,"photo1"会在控制器B上显示一张图片,而"photo 2"会在控制器B上显示不同的图片. 我可以在现有 table 视图代码中实现什么代码以发送到第二个控制器?

import UIKit

class adultcardiaclist: UITableViewController {

    let adultcardiac = ["photo1", "photo2", "photo3"]



    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

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

        cell.textLabel?.text = adultcardiac[indexPath.row]

        return cell
    } 
}

在您的 didSelectRowAt indexPath 中,添加以下行

 self.performSegue(withIdentifier: "segue", sender: nil)

您应该在第二个 viewcontroller

中声明 imageUrl 变量
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

            if segue.identifier == "segue" {

                if let indexPath = tableView.indexPathForSelectedRow {

                    let selectedRow = indexPath.row
                    let passingVal = segue.destination as! SecondViewController
                    passingVal.imageUrl = self.imageUrlArr[selectedRow] as? String
                }
         }
 }

在你的didSelectRowAt中将选择的索引图像传递给下一个viewController

func tableView(_ tableView: UITableView, didSelectRowAt
 indexPath: IndexPath){
    let controller = YourSecondVC(nibName:"YourSecondVC",bundle:nil)
    controller.selectedImageName = self.adultcardiac[indexPath.row]
   self.navigationController?.pushViewController(controller, animated: true)

}

在你的第二个ViewController中创建一个变量来接收第一个屏幕图像名称

var selectedImageName:String = ""

在你的第二个ViewController viewWillAppear 加载图像到 imageView

self.YOUR_IMAGE_VIEW.image = UIImage(named:"\(selectedImageName:String)")

希望对您有所帮助

首先,确保您在第一个和第二个视图控制器之间进行了切换(从 table 视图控制器到详细视图屏幕)。确保 segue 有一个 name.for 例如"imageScreen"

现在在您的第一个视图控制器的 didSelectRowAtIndexPath 事件中,像这样调用 segue。

performSegueWithIdentifier("imageScreen", sender: nil)

现在在第一个view controller的prepareForSegue方法中,你可以自定义它来发送更多的细节。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{       
   if(segue.identifier=="imageScreen")
   {
     let temp = segue.destinationViewController as! ViewControllerB
     temp.imageName = "Photo"
   }
}

假设您的详细信息屏幕的 class 称为 ViewControllerB,它有一个 属性 称为字符串类型的 imageName。您可以更新代码以使用您的真实视图控制器。

您可以使用 TableView 控制器中提供的默认委托

 import UIKit

class CustomTableController: UITableViewController {

    let adultcardiac = ["photo1", "photo2", "photo3"]

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

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return adultcardiac.count
    }


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

        cell.textLabel?.text = adultcardiac[indexPath.row]

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        let Vc = self.storyboard?.instantiateViewController(withIdentifier: "imageVC") as! imageVC

        switch indexPath.row
        {
        case 0:
            Vc.passedImage = UIImage.init(named: "screenShot")!
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        case 1:
            Vc.passedImage = UIImage.init(named: "screenShot1")!
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        case 2:
            Vc.passedImage = UIImage.init(named: "screenShot2")!
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        default:
            Vc.passedImage = UIImage.init(named: "screenShot")!
            self.navigationController?.pushViewController(Vc, animated: true)
        }
    }
}

-->我的imageVCClass

import UIKit

class imageVC: UIViewController {

    @IBOutlet weak var myImageView: UIImageView!

    var passedImage : UIImage! = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.myImageView.image = passedImage
    }
}

-->输出

---> 当 TableView Controller 加载到内存栈时

-->选中一行时

--> 当 DidSelect 执行并显示结果时 - New ImageVc with passed Image

--> 我的故事板

像这样使用didSelect委托方法

如果从代码创建

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       let selectedImage = adultcardiac[indexPath.row]
       let secondViewController = SecondViewController()
       secondViewController.image = selectedImage
       self.navigationController?.pushViewController(secondViewController, animated: true)
}

如果从故事板创建

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       let selectedImage = adultcardiac[indexPath.row]
       let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
       let secondViewController = storyBoard.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
       secondViewController.image = selectedImage
       self.navigationController?.pushViewController(secondViewController, animated: true)
}