TableView 在新控制器上显示不同的图像 [英] TableView to display different images on new controller
问题描述
我终于创建了一个 tableview
,它将填充一定数量的选项供用户点击.理想情况下,我希望用户单击一行,这将根据用户所做的选择在第二个控制器上显示图像.例如,"photo1" 会在控制器 B 上显示 1 张图片,而 "photo 2" 会在控制器 B 上显示不同的图片.我可以在现有的代码中实现哪些代码?表视图代码发送到第二个控制器?
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
}
}
推荐答案
可以利用 TableView Conroller 中提供的 Default Delegate
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)
}
}
}
--> 我的 imageVC 类
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 控制器加载到内存堆栈中时
---> When TableView Controller Loaded in memory Stack
--> 当一行被选中
--> 当 DidSelect 执行并显示结果时 - New ImageVc with Passed Image
--> when DidSelect Execute and displays result - New ImageVc with passed Image
--> 我的故事板
这篇关于TableView 在新控制器上显示不同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!