0

I'm trying to modify a variable with the number of the row selected in a UITableView, so that I can access that variable from another UIViewController but when I'm setting the value of the variable with the row number in func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) it appears that the variable is not changed.

I've a View Controller with an UITableView, what I'd like is to select a row, then, when I click on a button a Popoverview appear where I can parameterize things linked to the row I selected.

Here is what I've done :

import UIKit

class Settings: UIViewController , UITableViewDataSource, 
UITableViewDelegate{

    @IBOutlet weak var tableView: UITableView!
    var RowSelected = Int()

    let animals = ["Tap", "Double Tap", "Long press", "Swipe up", "Swipe down", "Swipe left", "Swipe right", "Zoom", "Unzoom"]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell"/*Identifier*/, for: indexPath as IndexPath)
    cell.textLabel?.text = animals[indexPath.row]
    return cell
}

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    RowSelected = indexPath.row
    print(RowSelected)
}

}

It print the row perfectly here, but when I access it from the other ViewController it's always equal to 0.

import UIKit

class GestureConfiguration: UIViewController, 
UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var actionSelected: UILabel!

let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
var items = ["1", "2", "3", "4", "5"]
var index : Int = 1

var scVC = Settings()

let gestes = ["Tap", "Double Tap", "Long press", "Swipe up", "Swipe down", "Swipe left", "Swipe right", "Zoom", "Unzoom"]

@IBOutlet weak var tableView: UITableView!

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 9
}

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return self.items.count
}

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


    collectionView.allowsMultipleSelection = true

    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.myLabel.text = self.items[indexPath.item]
    cell.backgroundColor = UIColor(red:0.13, green:0.37, blue:0.58, alpha:0.7)
    cell.layer.borderColor = UIColor.black.cgColor
    cell.layer.borderWidth = 1

    return cell
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    let cell = collectionView.cellForItem(at: indexPath)
    print("You selected cell #\(indexPath.item + 1) section \(indexPath.section + 1)")
    cell?.backgroundColor = UIColor(red:0.08, green:0.28, blue:0.45, alpha:1.0)
    ///////// HERE \\\\\\\\\\
    actionSelected.text = String(scVC.RowSelected)
    print(scVC.RowSelected)
    // Always print 0, same for the label.
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    // handle tap events
    let cell = collectionView.cellForItem(at: indexPath)
    print("You unselected cell #\(indexPath.item + 1) section \(indexPath.section + 1)")
    cell?.backgroundColor = UIColor(red:0.13, green:0.37, blue:0.58, alpha:0.7)
}

What am I missing? When I hardcode a value to RowSelected (like 99), I'm able to see 99 in my second ViewController.

Thanks for your help.

EDIT for Akhilrajtr :

class Settings: UIViewController , UITableViewDataSource, UITableViewDelegate{

@IBOutlet weak var tableView: UITableView!
var RowSelected = Int()

let animals = ["Tap", "Double Tap", "Long press", "Swipe up", "Swipe down", "Swipe left", "Swipe right", "Zoom", "Unzoom"]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell"/*Identifier*/, for: indexPath as IndexPath)
    cell.textLabel?.text = animals[indexPath.row]
    return cell
}

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

//Not overriding any function, 
Override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "toPopover") {
        var secondViewContr =  segue.destination as! GestureConfiguration
        secondViewContr.scVC = self
    }
}
}

And:

class GestureConfiguration: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var actionSelected: UILabel!

var scVC = Settings()

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    let cell = collectionView.cellForItem(at: indexPath)
    print("You selected cell #\(indexPath.item + 1) section \(indexPath.section + 1)")
    cell?.backgroundColor = UIColor(red:0.08, green:0.28, blue:0.45, alpha:1.0)

    actionSelected.text = String(scVC.RowSelected)
    print(scVC.RowSelected)
}
Hawkydoky
  • 194
  • 1
  • 15

4 Answers4

1

if you are using segue to show the second view controller, then in Settings view controller implement the below method

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "your segue id") {
        var secondViewController =  segue.destinationViewController as! GestureConfiguration
        secondViewController.scVC = self
    }
}

if you just need the selected row identifier, then create a var in GestureConfiguration and set it from prepareForSegue

Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30
  • Yes I just need the selected row identifier. I'm new to swift, So I don't really understand what you mean by "set it from prepareForSegue" something like 'var varName = prepareForSegue' ? Looks weird to me. – Hawkydoky Aug 04 '17 at 12:10
  • you can implement `prepareForSegue` as above in Settings view controller. and instead of `"your segue id"` use the correct segue identifier from storyboard. if not set any identifier, you can set one by selecting that segue. then the above code will do the requirement. – Akhilrajtr Aug 04 '17 at 12:14
  • Same as bellow I've an instance member "scVC" cannot be used on type "GestureConfiguration". Plus It doesn't override a function. – Hawkydoky Aug 04 '17 at 12:32
  • the prepare for segue method signature was wrong, i've updated that in answer. `override func prepare(for segue: UIStoryboardSegue, sender: Any?)` try with this – Akhilrajtr Aug 04 '17 at 13:00
  • @Nikhlesh Bagdiya answered worked. But thanks for your help and time, you pointed thing I wasn't able to see :) – Hawkydoky Aug 04 '17 at 13:14
1

In GestureConfiguration create one variable of Settings class and then access it in your tableViewDelegate function.

In Settings class override function prepareforsegue as

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

        if let gestuedistination = segue.destination as? GestureConfiguration {
            gestuedistination.settings = self
        }
}

In GestureConfiguration class declare

@IBOutlet weak var actionSelected: UILabel!
var settings:Settings?

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // handle tap events
        let cell = collectionView.cellForItem(at: indexPath)
        print("You selected cell #\(indexPath.item + 1) section \(indexPath.section + 1)")
        cell?.backgroundColor = UIColor(red:0.08, green:0.28, blue:0.45, alpha:1.0)
        actionSelected.text = String(settings!.RowSelected)
        print(settings!.RowSelected)
}
Nikhlesh Bagdiya
  • 4,316
  • 1
  • 19
  • 29
  • I've an "instance member "settings" cannot be used on type "GestureConfiguration" error" on GestureConfiguration.settings = self; – Hawkydoky Aug 04 '17 at 12:27
  • @Hawkydoky check updated answer i just change gestuedistination.settings = self – Nikhlesh Bagdiya Aug 04 '17 at 12:39
  • Please debug the value of settings!.RowSelected and check it is 0 or not, if it is correct than you have change weak var actionSelected: UILabel! to var actionSelected: UILabel! and check the connection is correctly connected to storyboard or not. – Nikhlesh Bagdiya Aug 04 '17 at 12:56
  • sorry, I miss placed something in my code, it's ok, it's working. Thank you :) – Hawkydoky Aug 04 '17 at 13:12
0

It looks to me like the issue is because you are instantiating a new view controller in var scVC = Settings(). Instead, whenever you create this GestureConfiguration view controller, you should pass the reference to the proper Settings view controller so that RowSelected is what you want.

(Although it would be better just to pass RowSelected itself instead of the entire Settings view controller unless you need the view controller for other things)

0

Make globlal object of setting var globalSetVC = Settings() and in setting view controller save rowSelect like this

func tableView(_ tableView: UITableView, didSelectRowAt indexPath:IndexPath) {globalSetVC.RowSelected = indexPath.row print(globalSetVC.RowSelected)}

use it where ever require as globalSetVC.RowSelected