将图像从tableView传递到Swift中的viewController

时间:2016-12-26 19:04:37

标签: ios iphone swift uitableview uiimageview

我在Assets文件夹中有图像,我试图将数据从tableView传递到下一个viewController。我有两个表,我创建了UIImageView的插座。
ThirdView.swift包含struct
    导入UIKit

struct ThirdView {
var ThirdViewArray = [String]()
var Pic = [String]()
}

SecondTableViewController.swift包含

class SecondTableViewController: UITableViewController {

var ExerciseListArray = [String]()
var SecondAnswerArray = [String]()



override func viewDidLoad() {
    super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return ExerciseListArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var Cell = self.tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath) as UITableViewCell
    Cell.textLabel?.text = ExerciseListArray[indexPath.row]

    return Cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    var indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow! as NSIndexPath
    var DestViewController = segue.destination as! ExercisesViewController
    DestViewController.FirstString = SecondAnswerArray[indexPath.row]
    DestViewController.ExerciseImage = SecondAnswerArray[indexPath.row]
}


}

但是,SecondAnswerArray是一种String类型,我收到一个错误:
     "不能将typ String的值赋给UIImage"在线DestViewController.ExerciseImage = SecondAnswerArray [indexPath.row]
最后,FirstTableViewController.swift:     class FirstTableViewController:UITableViewController {

var BodyPartsArray = [String]()
var SecondArray = [SecondTable]()
var ThirdArray = [ThirdView]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    BodyPartsArray = ["Neck", "Shoulders", "Upper Arms", "Forearms", "Back", "Chest", "Waist", "Hips", "Thighs", "Calves"]

    SecondArray = [
    SecondTable(SecondTitle: ["Neck1","Neck2","Neck3"]),
    SecondTable(SecondTitle: ["Shoulders1","Shoulders2","Shoulders3"]),
    SecondTable(SecondTitle: ["Upper Arms1","Upper Arms2","Upper Arms3"]),
    SecondTable(SecondTitle: ["Forearms1","Forearms2","Forearms3"]),
    SecondTable(SecondTitle: ["Back1","Back2","Back3"]),
    SecondTable(SecondTitle: ["Chest1","Chest2","Chest3"]),
    SecondTable(SecondTitle: ["Waist1","Waist2","Waist3"]),
    SecondTable(SecondTitle: ["Hips1","Hips2","Hips3"]),
    SecondTable(SecondTitle: ["Thighs1","Thighs2","Thighs3"]),
    SecondTable(SecondTitle: ["Calves1","Calves2","Calves3"])]

    ThirdArray = [
        ThirdView(ThirdViewArray: ["NeckText1","NeckText2","NeckText3"], Pic: []),
        ThirdView(ThirdViewArray: ["ShouldersText1","ShouldersText2","ShouldersText3"], Pic: ["310","311","312"]),
        ThirdView(ThirdViewArray: ["Upper ArmsText1","Upper ArmsText2","Upper ArmsText3"], Pic: ["313","314","315"]),
        ThirdView(ThirdViewArray: ["ForearmsText1","ForearmsText2","ForearmsText3"], Pic: ["","",""]),
        ThirdView(ThirdViewArray: ["BackText1","BackText2","BackText3"], Pic: ["","",""]),
        ThirdView(ThirdViewArray: ["ChestText1","ChestText2","ChestText3"], Pic: ["","",""]),
        ThirdView(ThirdViewArray: ["WaistText1","WaistText2","WaistText3"], Pic: ["","",""]),
        ThirdView(ThirdViewArray: ["HipsText1","HipsText2","HipsText3"], Pic: ["","",""]),
        ThirdView(ThirdViewArray: ["ThighsText1","ThighsText2","ThighsText3"], Pic: ["","",""]),
        ThirdView(ThirdViewArray: ["CalvesText1","CalvesText2","CalvesText3"], Pic: ["","",""])]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return BodyPartsArray.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let Cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
    Cell.textLabel?.text = BodyPartsArray[indexPath.row]

    return Cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow! as NSIndexPath
    let DestViewController = segue.destination as! SecondTableViewController
    var SecondTableArrayTwo: SecondTable
    SecondTableArrayTwo = SecondArray[indexPath.row]
    DestViewController.ExerciseListArray = SecondTableArrayTwo.SecondTitle


    var ThirdAnswerArray: ThirdView
    ThirdAnswerArray = ThirdArray[indexPath.row]
    DestViewController.SecondAnswerArray = ThirdAnswerArray.ThirdViewArray
    DestViewController.SecondAnswerArray = ThirdAnswerArray.Pic
}

}

再次,我在行DestViewController.SecondAnswerArray = ThirdAnswerArray.Pic

上遇到了同样的错误

ExercisesViewController:
    导入UIKit

类ExercisesViewController:UIViewController {

@IBOutlet weak var TextView: UITextView!
@IBOutlet weak var ImageView: UIImageView!

var FirstString = String()
var ExerciseImage = String()

override func viewDidLoad() {

    TextView.text = FirstString
    ImageView.image = ExerciseImage

}
}

任何人都可以帮我解决这个错误,并告诉我在FirstTableViewController.swift ThirdArray中将图片从Assets加载到我的UIImageView的正确方法吗?
谢谢

2 个答案:

答案 0 :(得分:0)

  

无法在行上键入String类型的值来键入UIImage   DestViewController.ExerciseImage = SecondAnswerArray [indexPath.row]

错误消息告诉你一切。 ExerciseImageUIImage的类型,您要为其分配字符串。

首先创建UIImage对象,而不是将其分配给ExerciseImage

let image = UIImage(named: SecondAnswerArray[indexPath.row])
DestViewController.ExerciseImage = image

答案 1 :(得分:0)

如果您可以分享ExercisesViewController的代码以获得更好的建议,那会很好,但这可能会对您有所帮助:

首先, ExercisesViewController 中的变量 DestViewController.ExerciseImage 可能是UIImage类型,在您的数组 SecondAnswerArray 上包含String类型的对象。如此处所声明:

var SecondAnswerArray = [String]()

当您将 SecondAnswerArray 中的值指定给预期为 UIImage 的对象时,您会收到该错误

  

无法将String类型的值赋值为UIImage“

将ExercisesViewController.ExerciseImage的类型更改为 String 类型,这将解决您的第一个问题。

要从Asset文件夹设置图像,只需将图像文件名指定为 UIImage ,如:

let img = UIImage(named: "NAME_OF_YOUR_FILE_WITHOUT_EXTENSION")

然后将该图像设置为 UIImageView

imageView.image = img

希望有所帮助