简体   繁体   中英

passing array values between view [swift 3] [iOS]

i've been developing apps in swift 3,
kinda new here (swift programming),
so here's the thing,
i want to pass the data from an array in first table view controller to second table view controller and then in second table view controller i want to use the if function to show different image in table view based on the values that i've been using in first table view,
i'd already did the segue thing but it didn't work
here's my first table view controller code block

var FirstTableArray = [String]()
var SecondArray = [SecondTable]()

   override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    FirstTableArray = ["Alam","Sejarah dan Budaya","Buatan"]

    SecondArray = [SecondTable(SecondTitle:["11","12","13"]),
                   SecondTable(SecondTitle:["21","22","23"]),
                   SecondTable(SecondTitle:["31","32","33"])]
}

the "FirstTableArray" is the array that i wanna pass its values, here's other code snippet in the first table view controller for using the "FirstTableArray" values to show the label in table view

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let Cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CostumTableViewCell

    Cell.judulGambar.text = FirstTableArray[indexPath.row]
    Cell.namaGambar.image = UIImage(named: gambarMenu[indexPath.row])

    Cell.namaGambar.layer.cornerRadius = Cell.namaGambar.frame.size.width/2
    Cell.namaGambar.clipsToBounds = true

    return Cell
}

now that i've been successfully passing the "SecondArray" values using this prepare for segue method but i can't do it for the "FirstTableArray" values here's my segue method

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    var IndexPath : IndexPath = self.tableView.indexPathForSelectedRow!
    let DestViewController = segue.destination as! SecondTableViewController

    var SecondArrayTableTwo : SecondTable

    SecondArrayTableTwo = SecondArray[IndexPath.row]
    DestViewController.SecondArray = SecondArrayTableTwo.SecondTitle

}

and now here's my second table view controller that i want to pass the first table view controller data to this view, my second array data has been passed its value, but i cannot do it for the "FirstTableArray" values my second table view controller code

   var namaGambarDetail = ["gunungmahawu.jpg","pantaimalalayang.jpg","pulaumanadotua.jpg","pulausuladen.jpg"]
var gambarSejarah = ["pantaimalalayang.jpg","gunungmahawu.jpg","pulaumanadotua.jpg","pulausuladen.jpg"]

var FirstArray = [String]()
var SecondArray = [String]()


override func viewDidLoad() {
    super.viewDidLoad()
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return SecondArray.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let Cell = self.tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath) as! CostumTableViewCell

    Cell.judulDetail.text = SecondArray[indexPath.row]

    Cell.gambarTampilDetail.image = UIImage(named: namaGambarDetail[indexPath.row])

    Cell.gambarTampilDetail.layer.cornerRadius = Cell.gambarTampilDetail.frame.size.width/2
    Cell.gambarTampilDetail.clipsToBounds = true



    return Cell
}

i want to pass the "FirstTableArray" value data when the row has been clicked, like an indexpath for selected values so i can change the image based on clicked values in first view with an if statement function like this

if(FirstTableArray.text.contains("my first table view array values")){ Cell.gambarTampilDetail.image = UIImage(named: namaGambarDetail[indexPath.row])}

sorry for my long post, hope u guys understand what i mean, sorry for my bad english tho, cmiiw, thanks, any help is appreciated :)

从SecondTableViewController删除此行

var SecondArray = [String]()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM