我最终创建了一个tableview
,其中将填充一定数量的选项供用户点击。理想情况下,我希望用户单击一行,这将根据用户的选择在第二个控制器上显示图像。例如," photo1" 会在控制器B上显示1张图片,而"照片2" 会在控制器上显示不同的图片B.我可以在现有的表视图代码中实现哪些代码发送到第二个控制器?
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
}
}
答案 0 :(得分:1)
您可以使用TableView Conroller
中提供的默认代理From java level I fixed this issue by chaning the time zone to America/New_York time
Long time = new Long(System.currentTimeMillis() );
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
Date date = new Date(sdf.format(new Date(time)));
long utcDateInMilliSeconds = date.getTime();
params.put("timestamp", new Long(utcDateInMilliSeconds/1000));
<强> - &GT;我的imageVC类
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)
}
}
}
<强> - &GT;输出强>
---&GT;当TableView Controller加载到内存堆栈
时- &GT;选择行时
- &GT;当DidSelect执行并显示结果时 - 新的ImageVc与传递的图像
<强> - &GT;我的故事板
答案 1 :(得分:0)
在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
}
}
}
答案 2 :(得分:0)
在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 = ""
在你的secondViewController的viewWillAppear中 将图片加载到imageView
self.YOUR_IMAGE_VIEW.image = UIImage(named:"\(selectedImageName:String)")
希望这会对你有所帮助
答案 3 :(得分:0)
首先,确保您的第一个和第二个视图控制器之间存在segue(从表视图控制器1到详细信息视图屏幕)。确保segue具有名称。例如&#34; imageScreen&#34;
现在在你的第一个视图控制器的didSelectRowAtIndexPath事件中,像这样调用segue。
performSegueWithIdentifier("imageScreen", sender: nil)
现在,在第一个视图控制器的prepareForSegue方法中,您可以对其进行自定义以发送更多详细信息。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if(segue.identifier=="imageScreen")
{
let temp = segue.destinationViewController as! ViewControllerB
temp.imageName = "Photo"
}
}
假设您的详细信息屏幕的类名为ViewControllerB,它有一个名为imageName of String type的属性。您可以更新代码以使用真实的视图控制器。
答案 4 :(得分:0)
使用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)
}
如果从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)
}