1

I tried to pass array to next view controller from my table view..

here is my code this far.

 @IBAction func done(_ sender: Any) {
    let view = self.storyboard?.instantiateViewController(withIdentifier: "pilihtabel") as! labelpilih
        for index in 0 ... (tabelpilihan.indexPathsForSelectedRows?.count)! - 1{
        let selectedrow = tabelpilihan.indexPathsForSelectedRows?[index].row
        print(pilihan[(tabelpilihan.indexPathsForSelectedRows?[index].row)!])
        view.pilih = pilihan[selectedrow!]

    }

    self.navigationController?.pushViewController(view, animated: true)
}

it works , but only pass the last row i picked.

2
  • For pass multiple records in tableview multiple selection allows = true. and then select multiple selection data to your next view controller . Commented Dec 10, 2016 at 4:36
  • @AlbertWu hey what's the pilihan in your case? Commented Sep 9, 2017 at 6:14

2 Answers 2

3

Another Way to pass it

@IBAction func done(_ sender: Any) {
let view = self.storyboard?.instantiateViewController(withIdentifier: "pilihtabel") as! labelpilih

        let arrPassing = NSMutableArray()
        for index in 0 ... (tabelpilihan.indexPathsForSelectedRows?.count)! - 1{
            let selectedrow = tabelpilihan.indexPathsForSelectedRows?[index].row
            arrPassing.addObject(pilihan[selectedrow!])
            print(pilihan[(tabelpilihan.indexPathsForSelectedRows?[index].row)!])

        }
        view.pilih = arrPassing
        self.navigationController?.pushViewController(view, animated: true)
Sign up to request clarification or add additional context in comments.

3 Comments

what is the different between NSMutableArray and Array or NSArray?
@AlbertWu In objective C NSArray is used to define an array which cannot be altered/changed later while NSMutableArray is used to define an array which can be altered/changed later on
@AlbertWu since in swift constants are defined used let and variables by var hence Array type variable will be of mutable type if it is defined using var and immutable if define using let
2

In the line view.pilih = pilihan[selectedrow!] you are assigning the value pilihan[selectedrow! to your next view's pilih variable.

But the pilih is not of array type so at the end of the loop the last value is overwritten to it and hence it only contains the last value.

To solve this you should make it array type and should append the values like

view.pilih.append(pilihan[selectedrow!])

10 Comments

thank you, it's work perfectly. Can you explain a little bit about append ? how can i miss that
@AlbertWu you needed to pass some values from pilihan (and not the single value) hence the next view's pilih variable must be an array type so you can to add those values in the pilih variable, therefore "append" is used to add those values one by one.
when i tried to change my pilih in next view's to Array type. it shows "",array1,array2. this is how i declare pilih to array type 'var pilih : Array = [""]'
you should change that line to var pilih = Array(), this will initialise the variable without any element.
okay thank you so much. I am really a newbie, and must learn a lot . i will keep in mind about that "append" btw.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.