I've created a pretty simple test app. It includes a 3 view controllers. The main view controller has a table view, that uses custom cells. The other two view controllers are able to be accessed through the main view controller, each have collection views, and can go back to the main view controller.
Here is the memory issue. Anytime I click on any of the cells from the 3 view controllers, the memory usage increases. I ran the app while using the 'Leaks' profiling template and found no leaks. Also used the 'Allocations' profiling template, checked 2 of the view controllers (recorded the reference counts), and all the stored reference counts under my program were released.
I haven't been able to use the Debug Memory Graph as it keeps crashing Xcode...
Main View Controller
import UIKit
class TableViewController: UITableViewController, UISearchBarDelegate {
#IBOutlet weak var searchForTool: UISearchBar!
#IBOutlet weak var toolTable: UITableView!
var searchActive : Bool = false
var data = [" Alphabetical", " Numerical"]
var identities = ["A", "B"]
override func viewDidLoad() {
super.viewDidLoad()
toolTable.delegate = self
toolTable.dataSource = self
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.toolLabel.text = data[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
}
One of the other View Controllers (both identical)
import UIKit
class AlphabeticalViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
#IBOutlet weak var collectionView: UICollectionView!
var labelArray = [String]()
var identities = [String]()
override func viewDidLoad() {
super.viewDidLoad()
labelArray = ["Main", "Definitions", "Steps", "References", "Other"]
identities = ["C", "B", "B", "D", "E"]
self.navigationController?.setNavigationBarHidden(true, animated: false)
collectionView.delegate = self
collectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return labelArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let myLabel = cell.viewWithTag(1) as! UILabel
myLabel.text = labelArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
}
Custom Cell Class
import UIKit
class CustomCell: UITableViewCell {
#IBOutlet weak var toolLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
I will provide any other images if needed. Any help would be greatly appreciated.
Information I got using the allocation tool.
The commented out code is just a search feature that I don't need atm.
The problem seems to be inside the CustomCell, maybe some resources not deinitialized.
Do you have some code inside awakeFromNib() or setSelected(...)?
You have problem with your tableView and collection view. look at IBOutlets your tableView name is toolTable
#IBOutlet weak var toolTable: UITableView!
but inside your datasource for the tableView you're accessing the wrong tableView
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// this is the wrong tableView
/*
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
*/
// you should use the tableview which you have declared
let cell = toolTable.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.toolLabel.text = data[indexPath.row]
return cell
}
You have the same problem with your CollectionViewControllers as well.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//accessing wrong collectionView
/*
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
*/
// here you have to use self because your have named your collectionView the same as collectionView
let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let myLabel = cell.viewWithTag(1) as! UILabel
myLabel.text = labelArray[indexPath.row]
return cell
}
Note: Your TableViewController and CollectionViewController are already Controllers. wondering why you have another tableView and collection view IBOutlets. Use one at a time
Firstly, I want to give credit to #totiG for pointing out that the problem could be with the navigation controller, and he was right.
There are other smaller memory issues, but the biggest by far had to do with my navigation. I kept pushing controllers onto the navigation stack, without popping them.
Here is the code for my final solution.
I replaced:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
With:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 0 {
if let navController = self.navigationController {
for controller in navController.viewControllers {
if controller is TableViewController {
navController.popToViewController(controller, animated: true)
}
}
}
} else {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
}
So now instead of pushing from controller to controller without popping any of them off of the stack, I pop all of the previous controllers up to the 'TableViewController' when 'Main' is clicked.
Related
There is a very similar question here, but the solution doesn't solve anything for me, mainly because my embedded collection view is already inside the table view cell's content view (I created it in storyboard).
Is there some setting that I need to check to allow my collection view to scroll? It seems that the parent table view cell is eating up all gestures.
TableViewController.swift
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDataSource, UICollectionViewDelegate {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
cell.collectionView.dataSource = self
cell.collectionView.delegate = self
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)
return cell
}
}
CustomTableViewCell.swift
class CustomTableViewCell: UITableViewCell {
#IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
You can find my demo project here
https://github.com/MattiaPell/CollectionView-inside-a-TableViewCell
Turns out in my case it was simple as not having User Interaction Enabled check marked for my child collection view.
I am trying to navigate to another ViewController from Collectionview cell which is inside Tableview cell.
I am trying to achieve using delegate method but it's not navigating to intended viewcontroller.
Here is code that i have developed so far. I am using xib setup here.
// ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate,CustomTableViewCellDelegate {
#IBOutlet weak var tableView: UITableView!
var customTableViewCell = CustomTableViewCell()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
customTableViewCell.delegate = self
tableView.delegate = self
tableView.dataSource = self
self.tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTableViewCell")
}
override func viewWillAppear(_ animated: Bool) {
navigationController?.navigationBar.barTintColor = UIColor.black
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
self.navigationController?.navigationBar.tintColor = UIColor(red: 211/255, green: 86/255, blue: 50/255, alpha: 1.0)
}
//Delegate method
func passTheCurrent(tableIndex: Int, collectionViewIndex: Int) {
print("collectionViewIndex \(collectionViewIndex)")
let selectpile = ObjectSceneViewCtrl()
self.navigationController?.pushViewController(selectpile, animated: true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
return customCell
}
}
Here is CustomTableViewCell where i am defining delegate methods. I am calling delegate function inside collectionview didSelectItemAt method. But delegate is returning nil.
import UIKit
protocol CustomTableViewCellDelegate {
func passTheCurrent(tableIndex: Int, collectionViewIndex: Int)
}
class CustomTableViewCell: UITableViewCell {
#IBOutlet weak var collectionView: UICollectionView!
var delegate: CustomTableViewCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.register(UINib.init(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
extension CustomTableViewCell : UICollectionViewDelegate {}
extension CustomTableViewCell : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 15
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath as IndexPath) as! CollectionViewCell
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.passTheCurrent(tableIndex: 0, collectionViewIndex: indexPath.item)
}
}
When i set break point, delegate is returning nil. What is wrong with this setup. please help me out.
do the following simple steps this may helps you
1- in your cellForRowAt method assign delegate to your cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
customCell.delegate = self
return customCell
}
2- in your CustomTableViewCell class update your didSelectItemAt method with following code
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let del = self.delegate
{
del.passTheCurrent(tableIndex: 0, collectionViewIndex: indexPath.item)
}
}
Basically what we are doing now 1st we assigning delegate to cell on each index. 2nd in table cell class first we checking is delegate confirmed then pass the data to parent controller class. 😊
I want to know that how to show index path both table view and collection view index. In my case table view cell contain many collection view cell. And collection view cell contains one button when user pressed this button an alert show which show the pressed button from table view index (i.e 2) and collection view index (i.e 4).
How do that ?
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK:- Tabel View
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CellForTableView
return cell
}
//MARK:- Collectio View
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 8
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CellForCollectionView
cell.myIndexPath = indexPath
cell.TabedDelegate = self
return cell
}
}
extension ViewController : collectionViewCellTabbedDelegate{
func clickBtnTabbed(indexPath: IndexPath) {
print(indexPath)
let alert = UIAlertController(title: "Show Index Number!", message: "Selected Collection View Index Number is \(indexPath[1])", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
Here is my Collection View Cell:
import UIKit
protocol collectionViewCellTabbedDelegate {
func clickBtnTabbed(indexPath: IndexPath)
}
class CellForCollectionView: UICollectionViewCell {
var myIndexPath : IndexPath!
var TabedDelegate : collectionViewCellTabbedDelegate?
#IBOutlet weak var clickBtnOut: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
self.clickBtnOut.addTarget(self, action: #selector(clickBtnAct(_:)), for: .touchUpInside)
}
#IBAction func clickBtnAct(_ sender: Any) {
TabedDelegate?.clickBtnTabbed(indexPath: self.myIndexPath)
}
}
I don't know your realization, but from my point of view I would recommend to use ColletionView instead of TableView because its more flexible. As I understood you have collection view inside table view cell. To get IndexPath you can use delegate to, and after tap on cell you can pass your indexPath via delegate.
If you want to access indexPath of cell inside cell, you can try to access your superview (checkout Stackoverflow) or you can use code below
extension UIResponder {
func next<U: UIResponder>(of type: U.Type = U.self) -> U? {
return self.next.flatMap({$0 as? U ?? $0.next() })
}
}
extension UICollectionViewCell {
var collectionView: UICollectionView? {
return self.next(of: UICollectionView.self)
}
var indexPath: IndexPath? {
return self.collectionView?.indexPath(for: self)
}
}
customize these code your own way
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TypeCell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath as IndexPath) as! TypeCel
cell.CollectVw.dataSource = self
cell.CollectVw.delegate = self
cell.CollectVw.tag = indexPath.row
cell.CollectVw.reloadData()
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let dt = array[collectionView.tag] as! NSArray
return dt.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:collectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! collectionCell
let datGpAry = array[collectionView.tag] as! NSArray
let dat = datGpAry[indexPath.row] as! NSDictionary
cell.button.addTarget(self, action: #selector(btnClk), for: .touchUpInside)
cell.button.tag = collectionView.tag
cell.button.accessibilityValue = String(indexPath.row)
return cell
}
#objc func btnClk(sender:UIButton) {
let index = sender.accessibilityValue!
}
I am working on a meme generator app. The meme generator is presented modally and is built into a tab bar view controller. The first tab displays all saved memes in a table view and the second is intended to display the saved memes in a collection view. The meme generator works as design and is saving generated memes to an array that is located in the App Delegate file (I know this is controversial, but it is a requirement of the exercise). I am attempting to set up the collection view and I am not able to get a reference to the meme object in the app delegate file and I don't understand why.
import UIKit
class CollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
#IBOutlet weak var collectionView: UICollectionView!
var memes: [Meme]! {
didSet {
collectionView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
memes = appDelegate.memes
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return memes.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
cell.imageView.image = memes[indexPath].memedImage
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}
I based this code on the code used for my table view, which works as designed. That code is:
import UIKit
class TableViewMemesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var memes: [Meme]! {
didSet {
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
memes = appDelegate.memes
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return memes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let meme = memes[indexPath.row]
cell?.imageView?.image = meme.memedImage
cell?.textLabel?.text = meme.topText
return cell!
}
}
How can I get a reference to the array data and display it inside of the collection view when the user saves a new meme? Here is a link to the repo.
You have some issues, first you have to create a Custom CollectionViewCell:
class Cell: UICollectionViewCell {
#IBOutlet var imageView:UIImageView!
}
remember to set the Custom Class for UICollectionViewCell inside the storyboard:
then when you dequeue the cell, be sure of as! Cell:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell
cell.imageView.image = memes[indexPath.row].memedImage
return cell
}
Then inside your TabBarStoryboard.storyboard, you must set delegate and datasource for your CollectionViewController (I checked your github, you didn't attach them).
Finally the result will be:
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)
}
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)
}
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
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)
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