variable is not updated when row selected in UITableView

时间:2017-08-04 11:56:34

标签: ios swift uitableview swift3

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)
}

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)
}

答案 2 :(得分: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)

答案 3 :(得分: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