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