I am trying to get the Json data to show up in a table view cell, I have no value showing up. Please let me know what I am doing wrong. My tableview is linked Correctly to my viewcontroller(both as datasource and delegate).
Note: I havent called self.tableview.reloadData() anywhere as of now. If this is what needs to be done, Please tell me where I have to call it. I tried on viewdidload() it didnt work.
//json data
if let mydata = data {
do{
let myJson = try JSONSerialization.jsonObject(with: mydata, options: .mutableContainers) as AnyObject
if let articlesFromJson=myJson["account"] as! NSDictionary?{
self.points = articlesFromJson["Points__c"] as! Double
self.tier = articlesFromJson["Member_Tier__c"] as! String
//My destination VC is called HomeViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "HomeViewController" {
if let destVc = segue.destination as? HomeViewController {
destVc.pointrs = self.points
destVc.tiers = self.tier
}
}
}
//tableview
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MemberInfoTableViewCell
cell.points.text = String(pointrs)
cell.tier.text = tiers
return cell
}
UPDATE : I just redid it, and it works. I must have missed something tiny! And The code seems to be correct. I also added a navigation controller so the segue code changed, nonetheless. It works. Thank you!
1) Make Sure your variable "pointrs" is initialized
2) you do not need to call table.relaod() when you are coming on the table view controller for the first time
3)make sure you have pass correct count in below delegate
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fruits.count //Array Count
}
Try this,, pass the values through a function to the "TableViewCellClass" you created and then , set the values of labels from there...
Make sure you indeed pass data into your table view controller because your table view controller looks okay. When you pass data, your code is
if segue.identifier == "HomeViewController" {
if let destVc = segue.destination as? HomeViewController {
destVc.pointrs = self.points
destVc.tiers = self.tier
}//Your code can ends up here
}//Your code can ends up here
See my comments from the code. If any of these if statements are false, you are not passing data. So first put a break point here to figure out if you are actually passing data or not.
If you are indeed passing data, put a break point in your cellForRow method to again make sure both your pointrs and tiers are valid with correct value
Related
I'm currently making a to do list app using Swift 4. The home view controller has a tableview with some categories in it and when one is selected, it takes the user to a view controller where the items in that category are listed. I have a bug however as only the most recent item is showing in the list.
I think this is due to the way I am navigating to the list view controller. I am currently doing this:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let destinationVC = ListVC()
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedCategory = categoryArray[indexPath.row]
}
navigationController?.pushViewController(destinationVC, animated: true)
tableView.deselectRow(at: indexPath, animated: true)
}
And in the list view controller, I just have this to load the data:
var selectedCategory : Category? {
didSet {
loadItems()
}
}
I firstly created this app using storyboards and when using segues, it worked completely fine.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToItems", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! TodoListVC
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedCategory = categoryArray[indexPath.row]
}
}
So basically, the problem is that in the secondary list view controller, it will only show the most recently added item and no other ones even when they are stored in core data. I think it is to do with the way I am showing the secondary view controller as I am creating a new object every time.
How to properly go to the next view controller?
Remove the segue and add the storyboard id
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "storyboard_id") as! TodoListVC
vc.selectedCategory = categoryArray[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
Try this it will help you:-
You can send data from one view controller to another using storyboard
instance.
let next = self.storyboard?.instantiateViewController(withIdentifier: "NextControllerStoryBoard_id")as! NextController
next.str = "data which you want to pass"
self.navigationController?.pushViewController(next, animated: true)
here NextController is your controller class name where you want to go.str is the string name which you declare on NextController like
let str = String()
you are able to send string in that variable in same way you send any thing array dictionary ,image, Int value etc.
NextControllerStoryBoard_id is id which you declare at storyboard of that controller
In storybard id add your storybard id
Hope this will help you
I think that with this chunk of code I already sensed that you are passing data to the other view controller the incorrect way:
let destinationVC = ListVC()
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedCategory = categoryArray[indexPath.row]
}
...
What I would suggest is that, instead of passing the data this way, you have to pass an array containing the items within the selected category using an array, then pass that array via the prepare for segue.
Then from the viewdidappear or viewdidload method in the receiving view controller, use the passed array from the source VC and use that as a datasource for your table view within that 2nd VC.
In Swift 3, I'd like to pass the string contained in a cell of a UITableView into the subsequent View Controller.
As a note, I read several answers to this that seem to be specific to earlier versions of Swift, so I wanted to reopen. As an example, the last solution here looked straightforward, but I can't get it to work.
In TableViewController, I create a string of an audio file's name and date posted with this JSON extraction:
if let shows_list = json as? NSArray
{
for i in 0 ..< data_list.count
{
if let shows_obj = shows_list[i] as? NSDictionary
{
let episode_name = shows_obj["episode"] as? String
let episode_date = shows_obj["date"] as? String
TableData.append(episode_date! + " | " + episode_name!)
let testString = TableData.append(episode_date! + " | " + episode_name!)
// the part above runs and will add the string into each cell. now here, I want to pass the value of that string into a UILabel called episodeTitle in the target view controller
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let indexPath = self.tableView.indexPathForSelectedRow {
let destinationVC = segue.cellPasser
EpisodeViewController.text = testString //this is the string in second view controller
}
}
}
}
}
This is throwing two errors:
Value of type 'UIStoryboardSegue' has no member 'cellPasser'
In the Storyboard, I have added an identifier to the Show segue called 'cellPasser.' My belief is I should be able to call that here but it's being flagged.
EpisodeViewController.episodeTitle = testString
I have an outlet in EpisodeViewController that is called episodeTitle, so I am wondering if there is a preferred way other than this to pass strings into an element like that.
I'm new to Swift -- hoping someone sees a better way to do this.
Edit: In case useful, here's the View Controller linked from the segue.
class EpisodeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBOutlet var episodeTitle: UILabel!
Here are steps to pass value from TableViewController to next ViewController :
in TableViewController You should declare a didSelectRowAt method.
Declare a method prepare for segue
Do not forget to declare a variable in second view Controller.
1.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "Identifier", sender: indexPath)
}
2.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Identifier" {
let vc = segue.destination as! NameOfYourViewController
vc.variableInSecondVc = variableInTableView
}
}
3.
var variableInSecondVc = ""
i am wondering how I can pass data from a ViewController to a cell?
I am a beginner so I may oversee the obvious :P
I have a home VC and when you press on a commentButton you get to the CommentVC which holds the postId of the post.
As I want to be able to like a comment( which works perfectly) and to notice the user about his comment being liked(which does not work for now) I need to have the postId not only in the commentVC ( which holds the correct one) but also in the cell.
this is the code where I pass data from the HomeVc to the CommentVC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "CommentSegue" {
let commentVC = segue.destination as! CommentViewController
let commentCell = CommentTableViewCell()
let postId = sender as! String
commentCell.postId = postId
commentVC.postId = postId
}
}
When I print out both variables in CommentVc and CommentCell, only the CommentVc shows the correct one whereas the Cell has "nil" as the print out statement.
Any Idea how I can pass it?
You shouldn't instantiate UITableViewCells yourself by calling your custom classes initialiser, but you should do it in your UITableViewController class (or a class that conforms to UITableViewContollerDataSource protocol).
Pass the data you want to show in your cells to your table view controller and in your data source methods (for example tableView(_:cellForRowAt:)) when creating your cell using dequeueReusableCell(withIdentifier:) assign the data to the specific cell.
You should not pass a table cell. Since you already passed the postId to your comment view controller, you can access to this id from a table view cell in your comment view controller in this way
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "EditorFontCell") as! EditorFontCell
print(self.postId)
//do what you want with postId with the current cell object here
return cell
}
Now remove the cell in segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "CommentSegue" {
let commentVC = segue.destination as! CommentViewController
let postId = sender as! String
commentVC.postId = postId
}
}
Where did you perform the print out statement? Typically, it is recommended to pass any data to the cell view in cellForRow of TableView delegate method. Inside the cell class, you can have a configure(_ myId: String) method with the id as one of the parm to be passed in. Then print it inside that method.
//In cell table cell class create a variable to hold the data. Here I made postId as String variable.
class EditorFontCell: UITableViewCell{
var postId: String! // postId may be other type
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
// in this method you create an object i.e. cell, of type EditorFontCell
// and you can access that postId by this tableCell object. You can simple assign a value to as shown below.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "EditorFontCell") as! EditorFontCell
cell.postId = "String data"
return cell
}
I am now passing value from VC1 to VC2
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
FIRDatabase.database().reference().child("Menus/Day1").observeSingleEvent(of: .value, with: {(snap) in
if let snapDict = snap.value as? Dictionary <String, AnyObject>{
let date = snapDict["mealPic"] as! String
let OrderInfo = DataService.ds.REF_ORDER
let USERUID = FIRAuth.auth()!.currentUser!.uid
let userinfo = OrderInfo.child(date).child(USERUID).child("data")
self.valueToPass = date
}
})
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "ToVari") {
var Vari = segue.destination as! Variation
Vari.passedValue = valueToPass
}
}
and my VC2 viewDidLoad:-
var passedValue : String!
override func viewDidLoad() {
super.viewDidLoad()
MEALDATE.text = passedValue
// Do any additional setup after loading the view.
}
It actually worked but the weird thing is the "passedValue" in VC2 didn't show at first (I select cell in VC1 to performSegue to VC2)
I had to go back to VC1 and then tap the cell to VC2 again to let "passedValue" showing what it supposed to be there.
anyone know why is this happening ?
This might be due to call to Firebase that is async (not 100% sure). Also You are calling segue from storyboard so I would suggest following:
Remove segue from storyboard
in your response from Firebase, add following: self.performSegueWithIdentifier("ToVari", sender: self)
and you should be fine.
This is to do with the view controller life cycle. Simply put, when you perform a segue to another view controller, the app loads the view of the next view controller, then shows it. You need to keep this in mind when overriding prepareForSegue. At this point, the view of the next view controller has already been loaded, and in the case of a table, populated with what is essentially no data. To fix your problem, you simply need to tell the table to reload when the view appears. Just call tableView.reloadData() in viewWillAppear on your new view controller. :)
This is due to Firebase async call. You can follow #NickCatib solution or
func navigateToNextViewController(valueToPass: String) {
let variation = self.storyboard?.instantiateViewControllerWithIdentifier("Variation") as! Variation
variation.passedValue = valueToPass
self.navigationController?.pushViewController(variation, animated: true)
}
Call this function in Firebase async, pass value you want to it as
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.navigateToNextViewController(date)
})
Go to storyboard, select viewController, under 'identity inspector'->identity->storyboardId -> give the identifier. This should be same as it is given in above function. (ex, give identifier name same as viewController name).
Hope this will help you.
Make sure you perform segue after this line has been executed :
self.valueToPass = date
only then you can call(Swift 3):
performSegue(withIdentifier: "ToVari", sender: Self)
Just change the Method of tableview to:
func tableView(tableView: UITableView, willSelectRowAtIndexPath
indexPath: NSIndexPath) -> NSIndexPath? { //put your code here
}
I have a tableView with 7 rows, each row is a different action. When a user taps on a row, a view controller is shown as a popover and a text field is displayed for them to add a note. I then want to save this note to Parse but I need to save the passedChildID with it and therefore it (passedChildID) needs to be passed from the tableviewcontroller. I have this working in other area's of my app from one tableView to another tableView but for some reason it won't work with the popover.
The following code is in SingleChildViewTableViewController.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "childToNote" {
let popoverViewController = segue.destinationViewController
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverViewController.popoverPresentationController!.delegate = self
let noteActionViewController = segue.destinationViewController as! actionNoteViewController
if let indexPath = tableView.indexPathForCell(sender as! UITableViewCell) {
noteActionViewController.passedChildID = passedChildID
}
}
if segue.identifier == "childToItemNeeded" {
//other stuff
}
}
This builds and runs fine, but when I tap on that particular row, I get the following error.
Could not cast value of type '<<app_name>>.SingleChildTableViewController' (0x10e1bef60) to 'UITableViewCell' (0x110bca128).
I have tried moving the let noteActionViewController... code above the let popoverViewController figuring that the former was never getting run but that didn't change anything.
The segue is being performed as followed (in case it makes any difference).
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 {
performSegueWithIdentifier("childToNote", sender: self)
}
}
I'm stumped because this code has worked elsewhere in my app without fail.
it looks like your problem lies in this line
indexPathForCell(sender as! UITableViewCell)
the sender in this case you are telling it to be self, which is a UIViewController
performSegueWithIdentifier("childToNote", sender: self)
You are then telling in the indexPathForCell line to cast it as a UITableViewCell but its of type SingleChildTableViewController. So change the sender to be the UITableViewCell
Maybe you can save the indexPath as a local variable and when you select a cell you change the value of the variable.