How to pass data from ViewControllers and show them into tableviewCell? - ios

I want to pass data from my ViewController to TableViewCell, How can I do that. How to pass data from one ViewController and show them in TableViewCell.

The question is a bit too vague, but I will try. If you are using a segue from another viewController and the data is, for example, an array of Strings i.e in your first viewController:
var arrayOfStrings = ["StringRow1", "StringRow2"] // etc
and you want to be able to access this array when you enter a new viewController and use it for your tableView then you can use prepareForSegue
First, in your viewController with the tableView have a variable ready for the array:
var arrayOfStrings = [String]()
Then you can pass the data over like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SegueToTableView" {
if let vc = segue.destination as? YourTableViewController
{
vc.arrayOfStrings = self.arrayOfStrings
}
}
}
Now when you arrive in your tableView viewController, your arrayOfStrings data will be passed.
This is a basic simple example as you did not provide much context.

Creat variable in your cell class and in cell for row just assign that value to variable and use in your cell class.

Related

how can I pass a swift array by reference between viewControllers in prepare(for segue:?

I know that swift array structs are copied by value and not reference. Is there a simple way that I can cast or otherwise pass the array by reference in my prepare(for segue:... method so that I may pass a reference to the array from viewController A to viewController B?
I have seen some answers that refer to passing an array to a function but I simply want to pass it to other viewControllers
Create wrapper class for your array and then pass that class reference wherever you want:
class ArrayWrapper {
let array: [YourType]
init(array: [YourType]) {
self.array = array
}
}
Yes you can achieve it by passing the values from viewController A to viewController B. All you need to do is, take an array (arrB) in ViewController B .
let VB = self.storyboard?.instantiateViewController(withIdentifier: "viewControllerB") as! viewControllerB
vc.arrA = arrA
self.navigationController?.pushViewController(vc, animated: true)
This will not pass reference but the values of area to arrB.
If u want to transfer array from one controller to another controller using segue create array in your first controller like this
var dataSource : [ANY MODEL CLASS] = []
use this method to transfer your data using segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "YOURSEGUE") {
let vc = segue.destination as! YOUCONTROLLER
vc.myArray = self.dataSource
}
in your second controller create same type array
var myArray : [ANY MODEL CLASS] = []
so now when you moved to another controller using this "YOURSEGUE" segue your array will have data
create a global array like this
var myGlobalArray = [Int]()
class viewControllerA : UIViewController {
}
now you can use it everywhere. but this is not a good thing
If you cast the array into NSArray it should do the work.

How to pass datasource to second View Controller? Swift

So I have a dataModel that pulls Arrays from a plist using my datasource class. In my view controller I call the datasource class with
let dataSource = DataSource()
and then I grab values from the array with
abilities = dataSource.ability[monster.ability! + 2]
My issue is when I grab the Datasource from the second Viewcontroller, there is a 2-4 second delay during the segue transition. How can I pass the datasource over the second viewcontroller, so i can read the values, without having to call let dataSource = DataSource() from within the second view controller?
There are a few different ways to handle passing information from one view controller to another.
However for your scenario I would recommend to create an array property in your second view controller that will hold your Datasource, but only by passing it from your first view controller by calling the prepare(for segue: UIStoryboardSegue, sender: Any?) method.
Here is an example of how I passed simple data to another view controller, pretty much a Master-Detail setup:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationViewController = segue.destination as? DetailNoteViewController
if segue.identifier == "toNoteDetailView" {
guard let indexPath = tableView.indexPathForSelectedRow else { return }
let note = self.notes[indexPath.row]
destinationViewController?.note = note
}
}

Passing data from embedded TableViewController

I have a TableViewController embedded in a ViewController. My UITableView has toggles. When one of these toggles are toggled I need my number on the ViewController to increase by 1. The issue I am having is not being able to get the text to change. I have supplied a screenshot to help better illustrate.
What I have tried doing is setting a variable var colorViewController: ViewController? in the TableViewController and then trying to set the text from there using colorViewController?.displayNumber.text = screenCount when toggling a toggle. screenCount is the variable that increasing as the toggles are toggled.
How are you setting your colorViewController variable?
You need to set it to the current instance of your ViewController in your ViewController's prepare for segue.
First, you'll need to give the embed segue an identifier. Select the segue in the storyboard and give it an identifier, say ColorPickerTableViewControllerSegue.
Then handle this segue in your ViewController (this is Swift 3 code):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ColorPickerTableViewControllerSegue" {
if let colorPickerTableViewController = segue.destination as? ColorPickerTableViewController {
colorPickerTableViewController.colorViewController = self
}
}
}

Not able to push data from one view controller to a table view controller

I ran into a strange problem while I was working on my project. (Please bear with me, I'm a Swift beginner.)
This is what my storyboard looks like so far.
On the left is my regular view controller, and on the right is the TableViewController. the transition inbetween has a specific ID.
**This is the first View Controller.**
class TestsMenuViewController: UIViewController {
var tests = [Test]()
var testTableViewController : TestsTableViewController?
override func viewDidLoad() {
self.tests = DataModel().patientData
//this just populates the array with some data.
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "testTableTransition" {
testTableViewController = segue.destination as? TestsTableViewController
testTableViewController?.tests = self.tests
print(testTableViewController?.tests[0].testDescription)
//somehow this is an empty array
}
}
somehow the test array does not get passed to the next controller :( what am i doing wrong?
You could save the data as NSUserDefaults and call the saved data to the view when you want
Hey I managed to solve my problem, what I just did was initialize the data that I pass to the TableViewController directly in the attributes. Then it worked!

How do I access an array from one viewcontroller in another viewcontroller

I am very new to the swift language and am trying to make an app that would do some basic calculations. The problem I am running into is that I want to use the values from an array that is declared in one viewcontroller in another viewcontroller to do calculations.
I declare the array in the first viewcontroller like this:
var array2 = [Double]()
And then I have no idea how to access this in the second view controller. I have tried looking at previous questions as well as online tutorials and have had no success.
Any help is greatly appreciated. Thank you in advance.
in VC2 create global var array2 = [Double]()
add following override func to body of VC1
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DVC = segue.destinationViewController as! ViewController2 //replace ViewController2 with the name of your 2nd VC
DVC.array2 = array2
}
Overall, what you are doing is telling VC1 to copy VC1's array2 to VC2's array2, before the segue happens.
As your app becomes more complex, if your VC1 has more than 1 segue...meaning can go to multiple different VC's...you'll need to change your prepareForSegue so that it takes into account for this.
One way to do this is to change it to
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SegueIdentifier you give to that particular segue in identityInspector" {
let DVC = segue.destinationViewController as! ViewController2 //replace ViewController2 with the name of your 2nd VC
DVC.array2 = array2
}
}

Resources