sending customCell label text to destination view - swift

I am stuck on this problem. I am downloading a json file from Firebase and successfully displaying in a TableView. That TableView has a custom cell with two labels. I have set up a segue link from the custom cell to a Detail ViewController. That works fine, but now I want to take the text content of the cell labels and send to a destination ViewController.
I have had no trouble doing this in the past, but am having trouble implementing this from the label.text in a custom cell.
I am using label tags (label1 and label2, although I may change that to the label names sometime).
The question is, how do I get the text contents from the two labels from the row selected and pass those to the DetailViewController? All my attempts so far have failed. Is there something I should be doing in the:
valueToPass = currentCell?.textLabel?.text
Here is the code:
struct postStruct {
let property : String!
let propType : String!
}
class TableViewController: UITableViewController {
var posts = [postStruct]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("users").queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let snapshotValue = snapshot.value as? NSDictionary
let property = snapshotValue!["property"] as? String
let propType = snapshotValue!["type"] as? String
self.posts.insert(postStruct(property: property, propType: propType), at: 0)
self.tableView.reloadData()
})
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].property
let label2 = cell?.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].propType
// print(posts)
// cell.setcell(valueToPass.label1)
return cell!
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
var valueToPass:String!
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow;
let currentCell = tableView.cellForRow(at: indexPath!) as UITableViewCell!;
valueToPass = currentCell?.textLabel?.text
performSegue(withIdentifier: "detailSegue", sender: self)
}
Any help most welcome, in particular code examples. Many thanks in anticipation

Create a custom class for your tableCell, and attach outlets in the StoryBoard.
the cell creation in cellForRowAt becomes
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") As! MyCustomCell
cell.label1.text = posts[indexPath.row].property
cell.label2.text = posts[indexPath.row].propType
and then didSelectRowAtIndexPath becomes
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!) as! MyCustomCell
self.valueToPass = currentCell.label1?.text
performSegue(withIdentifier: "detailSegue", sender: self)
The final thing you need if to prepare for the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "detailSegue" // you may have more than one segue, so you should check
{
let destinationController : DestinationViewControllerClass = segue.destination as! DestinationViewControllerClass
destinationController.valueToPass = self.valueToPass
}
}

Related

How to parse text from each cell to another View Controller UITextField

I have a class which is called InvoicesViewController "first image"
in that class there are cells that come from the API and each cell has a Label which represents a price of an item and a button near that price which segues you to PayViewController "second image"
and now i want that the UITextField in the PayViewController "second image" to be filled with the selected cell's label price.
I hope I am clear and someone guides me to the appropriate answer since i can't wrap my head around this one :)
This is my Code of the tableView :
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
let data = notifications.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].invoices
return data?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "invoicesCell", for: indexPath) as? invoicesModel else { return UITableViewCell() }
let currentNotifications = notifications.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].invoices
let currentInvoices = currentNotifications![indexPath.row]
cell.mainPriceLabelInvoices.text = "€\(currentInvoices.priceWithVAT ?? 0.00)"
return cell
}
you can use navigation code for move to the next page and pass the value with navigation.
let storyBoard : UIStoryboard = UIStoryboard(name: "PayViewController", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "PayViewController") as! PayViewController
nextViewController.txtfield.text = value
self.present(nextViewController, animated:true, completion:nil)
At first, create a var in PayViewController like this
var price : Double = 0.0
Now in viewDidLoad() method write down the below code
self.textField.text = "€\(price)"
then, you have to write the following code in cellForRowAt func of tableView in InvoicesViewController
cell.editBtn.addTarget(self, action: #selector(buttonPayTapped(_:)), for: .touchUpInside)
Now add the following function
#objc func buttonPayTapped(_ sender:UIButton) {
guard let indexPath = tableView.indexPathForView(sender) else {return }
let vc = self.storyBoard?.instantiateViewController(withIdentifier: "PayViewController") as! AddCategoriesVC
let currentNotifications = notifications.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].invoices
let currentInvoices = currentNotifications![indexPath.row]
// This is to transfer the selcted Data to Payment page
vc.price = currentInvoices.priceWithVAT ?? 0.00
self.present(vc, animated:true, completion:nil)
}
Add the following extension to get the exact indexPath
extension UITableView {
func indexPathForView(_ view: UIView) -> IndexPath? {
let center = view.center
let viewCenter = convert(center, from: view.superview)
let indexPath = indexPathForRow(at: viewCenter)
return indexPath
}
}

Getting data from the selected cell (TableView)

I have a TableView with several cells of data and there are 3 labels in each cell.
How can I save all 3 label.text into another variable with indexPath
let indexPath = self.tableView.indexPathForSelectedRow
Here is the full code
I've actually asked in another post that the variable "limit" becomes null after the .observe thing.
So I'm thinking if I can get the data directly from the cell.
import UIKit
import Firebase
import FirebaseDatabase
struct limitStruct{
var name : String!
var today : String!
var limit : String!
}
class CalcViewController: UITableViewController {
var limits = [limitStruct]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
navigationController?.navigationBar.barTintColor = UIColor.black
self.title = "Calculation"
navigationController!.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.white]
let databaseReference = FIRDatabase.database().reference()
databaseReference.child("Limit").queryOrderedByKey().observe(.childAdded, with: {
snapshot in
var snapshotValue = snapshot.value as? NSDictionary
let name = snapshotValue!["name"] as? String
snapshotValue = snapshot.value as? NSDictionary
let today = snapshotValue!["today"] as? String
snapshotValue = snapshot.value as? NSDictionary
let limit = snapshotValue!["limit"] as? String
snapshotValue = snapshot.value as? NSDictionary
self.limits.insert(limitStruct(name:name, today:today, limit: limit), at: self.limits.count)
self.tableView.reloadData()
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return limits.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Limit")
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = limits[indexPath.row].name
let label2 = cell?.viewWithTag(2) as! UILabel
label2.text = limits[indexPath.row].today
let label3 = cell?.viewWithTag(3) as! UILabel
label3.text = limits[indexPath.row].limit
return cell!
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails"{
let svc = segue.destination as! CirSliderViewController;
if let indexPath = self.tableView.indexPathForSelectedRow{
// svc.RsegueData =
}
}
}
}
You really don't want to be using viewWithTag(). The best way to handle this is to subclass UITableViewCell, with a property for your data model object
class LimitCell: UITableViewCell {
var limit: Limit {
didSet {
// configureCell()
}
}
}
Then in your view controller:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Limit", forIndex: indexPath) as! LimitCell
cell.limit = limits[indexPath.row]
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let svc = segue.destination as? CirSliderViewController, cell = sender as? LimitCell {
svc.RsegueData = cell.limit
}
}
It seems that you are using a callback to get the data. Does the data come from a server or is stored locally?
1) If the data comes from a server, you code cannot guarantee that var limits already got the data when the func prepare is called.
2) If the data is stored locally, and ONLY limitis nil, you must check whether or not you correctly assign limits[indexPath.row].limit to limits to the cell.(Is it nil at this moment?) I think the problem is in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) where limit is saved.
By the way, the more practical and efficient way to implement func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) is that:
Lets say the your custom cell is call LimitCell and has three UILabels: var label1, var label2, var label3.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Limit") as! LimitCell
cell.label1.text = limits[indexPath.row].name
cell.label2.text = limits[indexPath.row].today
cell.label3.text = limits[indexPath.row].limit
return cell
}

Swift 3 Breaks Cell For Row At Index Path

This code was from a now inactive tutorial that helped me load in data to a table view. Since the tutorial was written in Swift 2.0, I believe that this was changed in Swift 3. I know that the override function itself was changed, which I handled. But now, it brings me a Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0) error.
Update: I have tried multiple things including creating a custom class for the cell. I still either get the same error I listed above, or a Thread 1: Signal SIGABRT error on the first line of my App Delegate file. Creating a breakpoint hasn't helped me because I know where the error is coming from.
import UIKit
import Firebase
import FirebaseDatabase
struct postStruct {
let title : String!
let message : String!
}
class LoggedInController: UITableViewController {
var posts = [postStruct]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Posts").queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let snapshotValue = snapshot.value as? NSDictionary
let title = snapshotValue!["title"] as? String
let message = snapshotValue!["message"] as? String
self.posts.insert(postStruct(title: title, message: message), at: 0)
self.tableView.reloadData()
})
post()
}
func post(){
let title = "Title"
let message = "Message"
let post : [String : AnyObject] = ["title" : title as AnyObject,
"message": message as AnyObject]
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Posts").childByAutoId().setValue(post)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell")
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].message
let label2 = cell?.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].message
return cell!
}
}
Update 2: Here is the new code I used. It's not pretty and only gets the title.
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell?.textLabel?.text = posts[indexPath.row].title
cell?.detailTextLabel?.text = posts[indexPath.row].message
return cell!
} else {
let label1 = cell?.viewWithTag(1) as? UILabel
label1?.text = posts[indexPath.row].title
let label2 = cell?.viewWithTag(2) as? UILabel
label2?.text = posts[indexPath.row].message
return cell!
}
}
Using dequeueReusableCell, you are accessing cell which doesn't exists. To make your code work change the below line:
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
To
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
Ok this code let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") produces an optional called cell that may or may not contain a a valid instance of UITableViewCell. Optionals in Swift are a way to safeguard against nil values, you can read more about optionals here: Optionals
On the first run when your table view wants to load its data it calls all the required methods of your UITableViewDataSource. The first run is a critical one because there aren't any instances of the UITableViewCell the table view can dequeue yet. To solve your problem you have to do something similar to the code below:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell?.textLabel?.text = "New value"
cell?.detailTextLabel?.text = "New value"
return cell!
} else {
cell?.textLabel?.text = "" //reset value
cell?.detailTextLabel?.text = "" // resetValue
cell?.textLabel?.text = "New value"
cell?.detailTextLabel?.text = "New value"
return cell!
}
}
Code similar to the one above are usually used to programmatically add an instance of UITableViewCell. However, if you used interface builder to add a prototype cell use the let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) method to dequeue your cells in which case it does not return an optional and you do not need to do all the if / else blocks.
Something else I wanted to mention about your code is finding sub views buy their ID will not produce a very object oriented code and that maybe the source of your errors where the compiler can not find the sub views. The better way would be to use one of the built in instances of UITableViewCell such as .default or alliteratively you could subclass the said class and make your very own custom cells.
Hope this helped!
Try this
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
let label1 = cell.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].message
let label2 = cell.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].message
return cell
}
Edited
Make Sure you did these things
UITableViewController
In viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier:"catCell" )
let catNib : UINib = UINib.init(nibName: "TableViewCategoryCell", bundle: nil)
self.tableView.register(catNib, forCellReuseIdentifier: "TableViewCategoryCell")
In cellForRowAt indexPath
let cell : OPM_AlarmTableViewCategoryCell = tableView.dequeueReusableCell(withIdentifier:"OPM_AlarmTableViewCategoryCell" ) as! OPM_AlarmTableViewCategoryCell!
cell.categoryLabel?.text = "Some Text"
return cell
UITableviewCell.XIB
Hope u did these things
UITableviewCell
Make sure the dot appears so that the #IBOutlet is connected with the
xib label

Passing Data between TableView in Swift

I have two tableView running in my project.I am trying to pass(copy) my first tableViewcell data to second tableView.I using tableView row action method to pass data.My partial code below...
First VC:
var tableView: UITableView!
var DataArray = ["Bus","Helicopter","Truck","Boat","Bicycle","Motorcycle","Plane","Train","Car","S cooter","Caravan"]
var sendSelectedData = NSString()
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let copyAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Pass Data") { (UITableViewRowAction, NSIndexPath) -> Void in
print("Button Pressed") // Xcode Console prints **Button Pressed** when swipe action performed.
self.performSegue(withIdentifier: "send", sender: self)
}
return [copyAction]
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
self.performSegue(withIdentifier: "send", sender: self)
// segue.destination as! tableController
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!)!
self.sendSelectedData = (currentCell.textLabel?.text)! as String as NSString
let viewController = segue.destination as! tableController
viewController.labelcell = ([self.sendSelectedData as String])
print(self.sendSelectedData) // no result
}
Second VC:
var labelcell = [String]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: textCellIdentifier, for: indexPath as IndexPath) as UITableViewCell
cell.textLabel?.text = labelcell[indexPath.row] as? String
tableView.reloadData()
return cell
}
Above code looks like passing data to my second VC(segue).But, I am only getting a empty tableview..
Okay after testing it, it turns out, that you're using an incorrect prepareForSegue function. You are not using "prepareForSegue", you are creating a function called prepareForSegue - since the syntax has changed in Swift 3. This one will get called and you can pass data.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "send" {
let selectedIndex = sender as! NSIndexPath
let currentCell = tableView.cellForRow(at: selectedIndex as IndexPath)! as! Cell
self.sendSelectedData = (currentCell.label?.text)! as String as NSString
print(self.sendSelectedData) // till here it worked for me - it is filled with my label.text
// I don't know what this is "viewController.labelcell", so you have to to know how to go on from here on
viewController.labelcell = ([self.sendSelectedData as String])
}
}
Also you need to pass the indexPath:
self.performSegue(withIdentifier: "send", sender: indexPath)
Exactly like this:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let copyAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Pass Data") { (UITableViewRowAction, NSIndexPath) -> Void in
print("editActionsForRowAt called") // Xcode Console prints **Button Pressed** when swipe action performed.
self.performSegue(withIdentifier: "send", sender: indexPath)
}
return [copyAction]
}
This worked in my testing project.
Also beware: Cell is a custom subclass of UITableViewCell I have created and label is an UIOutlet of a label element for my test project.

Prepare for segue delay. Information is lagging but being presented

I'm pushing otherUserUid and otherUserFullName to another view controller but it isn't being call immediately. The information is lagging behind and it takes 2 clicks for the information to appear.
I think preparForSegue: is being called before didSelectRowAtIndexPath: Any solutions how to fix this?
Cheers!
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("jsqDirectory", sender: self)
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
self.otherUserFullName = (currentCell.textLabel?.text)!
print(self.otherUserFullName)
self.otherUserUid = (currentCell.detailTextLabel?.text)!
print(self.otherUserUid)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "jsqDirectory" {
if let viewController = segue.destinationViewController as? JSQViewController{
viewController.senderDisplayName = self.fullName
viewController.senderId = self.firebase.authData.uid
viewController.otherUid = self.otherUserUid
viewController.otherUser = self.otherUserFullName
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("directoryCell") as UITableViewCell!
let directoryItem = items[indexPath.row]
cell.textLabel?.text = directoryItem.fullName
cell.detailTextLabel!.text = directoryItem.key
cell.detailTextLabel!.hidden = true
return cell
}
in didSelectRowAtIndexPath you have to perform segue at the end of Function like thiss..
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
self.otherUserFullName = (currentCell.textLabel?.text)!
print(self.otherUserFullName)
self.otherUserUid = (currentCell.detailTextLabel?.text)!
print(self.otherUserUid)
self.performSegueWithIdentifier("jsqDirectory", sender: self)