我无法使用prepareForSegue
方法将图像和文本从tableView传输到另一个imageView。
foodimages
是一个包含类型字符串imageNames
的数组。
我也尝试过:
let imgName = "image\(index).png"
但失败了。
import UIKit
class TableView: UITableViewController,UITableViewDataSource,UITableViewDelegate {
var foodNames: [String] = ["Food1","Food2","Food3","Food4","Food5","Food6","Food7","Food8"];
var foodImages: [String] = ["image1", "image2","image3","image4","image5","image6","image7","image8"];
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("tblFoto", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = foodNames[indexPath.row]
var image : UIImage = UIImage(named: foodImages[indexPath.row])!
cell.imageView!.image = image
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
self.performSegueWithIdentifier("showIMG", sender: indexPath)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "showIMG") {
var destViewController: imgView = segue.destinationViewController as! imgView
var index = self.tableView.indexPathForSelectedRow()!.row
let imgName = foodImages[index] //"image\(index).png"
let data = UIImage(named: imgName)!
destViewController.photo = data
}
}
第二课 名为“imgView”的是
import UIKit
class imgView: UIViewController {
@IBOutlet var secView: UIImageView!
var photo: UIImage!
override func viewDidLoad() {
secView.image = photo
}
}
在调试区域中,它给出:
index(Int)7
imgName(String)“KLWR”
data(UIImage)0x000000010d667e10 0x000000010d667e10
答案 0 :(得分:0)
我认为这里发生的是您尝试在UIImage
中分配viewDidLoad
并在调用视图控制器中分配图像之前调用它。
你可以做的是这样的事情:
var image: UIImage? {
didSet {
self.secView.image = image
}
}
因此,您在目标视图控制器中显示图像属性,并在设置图像时更新图像视图。