问题
I have finally created a tableview
that will be populated with a certain amount of options for the user to click. Ideally, I would like the user to click a row, which will display a image on a second controller depending on the choice the user makes. For instance, "photo1" would display 1 picture on Controller B, while "photo 2" would display a different picture on Controller B. What code can I implement into my existing table view code to send to the second controller?
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
}
}
回答1:
You can make use of Default Delegate provided in TableView Conroller
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)
}
}
}
--> My imageVC Class
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
}
}
--> Output
---> When TableView Controller Loaded in memory Stack
--> When a Row is Selected
--> when DidSelect Execute and displays result - New ImageVc with passed Image
--> My StoryBoard
回答2:
First, make sure you have segue between your first and second view controllers (From the table view controller one to the Detail view screen). Make sure that the segue has a name.for e.g. "imageScreen"
Now in your first view controller's didSelectRowAtIndexPath event, Invoke the segue like this.
performSegueWithIdentifier("imageScreen", sender: nil)
Now in the prepareForSegue method of the first view controller, you can customize it to send more details.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if(segue.identifier=="imageScreen")
{
let temp = segue.destinationViewController as! ViewControllerB
temp.imageName = "Photo"
}
}
Assuming Your Detail screen's class is called ViewControllerB and it has a property called imageName of String type. You may update the code to use your real view controller.
回答3:
Use didSelect
Delegate Method Like this
if creted from code
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)
}
if creted from storyBoard
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)
}
回答4:
In your didSelectRowAt indexPath, add the following line
self.performSegue(withIdentifier: "segue", sender: nil)
You should declare the imageUrl variable in your second viewcontroller
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
}
}
}
回答5:
In Your didSelectRowAt
pass the selected index image to next 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)
}
In Your second ViewController create an variable to receive first screen image name
var selectedImageName:String = ""
In your secondViewController's viewWillAppear load image into imageView
self.YOUR_IMAGE_VIEW.image = UIImage(named:"\(selectedImageName:String)")
Hope this will help you
来源:https://stackoverflow.com/questions/48678032/tableview-to-display-different-images-on-new-controller