Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

skobuv

macrumors newbie
Original poster
Dec 10, 2016
10
0
I have images in Assets folder and Im trying to pass the data from tableView to next viewController.I have two tables, I created outlet to UIImageView.
ThirdView.swift contains struct
Code:
    import UIKit

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

SecondTableViewController.swift contains
  

    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]
    }
  
  
    }
However, SecondAnswerArray is a type of String, I get an error:
     "Cannot assign value of typ String to type UIImage" on the line   DestViewController.ExerciseImage = SecondAnswerArray[indexPath.row]


Finally, the 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
    }

    }
And again, I got the same error on the line DestViewController.SecondAnswerArray = ThirdAnswerArray.Pic

Can anyone help me to solve this errors and also show me the proper way to load pictures from Assets to my UIImageView in FirstTableViewController.swift ThirdArray?
Thank you
 

Ubuntu

macrumors 68020
Jul 3, 2005
2,160
491
UK/US
So secondAnswerArray is an array of Strings, where each string is the name of an image you have? In that case you'll need to create a UIImage out of it, by doing UIImage(named: exerciseImageString) (where exerciseImageString is the string in the array).

Also, as an improvement you could perhaps create a model object that stores the title & image string. You would then have one array (contains objects of that new class) and in cellForRow you'd ask it for it's title, while in prepareForSegue you'd ask it for it's image name (using the UIImage mentioned above)
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Tableviews should be supported by a data model that mimics the structure of the table. In a simple case like yours without sections you need an array of data objects where each data object represents a row in the table. A string is almost never going to be sufficient to model a row in a table. If you build an array of these model objects your code will become much simpler and easier to understand. You will pass an instance of one of these data model objects to the next view controller.

Something like this
Code:
struct RowData {
let title: String
let image: UIImage
let whateverElse
init(...)
}

If your table needs sections you can still have an array of arrays of these objects, or something similar to represent the data.
 
  • Like
Reactions: moonman239
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.