Click on profile from feed - swift - ios

I have a feed that is showing posts from the users that wanted to post something. What I am trying to do is display the name of the user that posted something in a button (that is working). Then when a user is pressing that button it should show another viewcontroller with the name of the button that the user pressed (that is semi-working).
What happens now is, when a user is pressing the name of another user from the post, the name that is showing in the other viewcontroller is not always the same as the name the user pressed.
I hope you understand :-)
Let me walk you through my code:
First viewController
before viewDidLoad, I am creating a string:
var sendName = "No name recieved"
Further down the page is this code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:updateTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! updateTableViewCell
let update = updates[indexPath.row]
cell.nameButton.setTitle(update.addedByUser, forState: .Normal)
sendName = (cell.nameButton.titleLabel?.text)!
}
My prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "showProfileFromFeed") {
let yourNextViewController = (segue.destinationViewController as! profileTableViewController)
yourNextViewController.viaSegue = sendName
}
}
The button title is being changed to what it should in the feed, but there must be something wrong with sendName because it doesn't always show the same name on the other viewController as the buttonTitle says..
Hope you can understand, otherwise please let me know :-)

It seems to me that where you update the sendName variable is the problem. The cellForRowAtIndexPath is called whenever a new cell is about to be displayed, or when the table is redrawn.
I don't know exactly how your app works, but if you can have multiple posts on the screen at the same time, then this means that the sendName variable would only be correct if you were pressing the button for the bottom-most post.
If you pressed the button for any other post then you would still get the name of the bottom-most post.
What I would recommend is using an IBAction that is triggered when the button is clicked to change the sendName variable. Then you can manually initiate the segue.

Related

swift didSelectAtIndexPath doesn't get triggered

First of all I would like to describe what actually I am trying to do.
I have two tableViewControllers - first one contains a list of bill's numbers and second one should displays the detail information about the bill depending on which cell of bill's number were selected.
In my first tableViewController I am using this to get bill's number from the cell and pass it to detailTableViewController:
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!) as! CustomBillTableViewCell
valueToPass = currentCell.billNumberLabel.text
print(valueToPass)
performSegue(withIdentifier: "billsDetailSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "billsDetailSegue"){
let detailController = segue.destination as! BillsDetailTableViewController
detailController.receivedValue = valueToPass
}
}
The problem is that didSelectRowAt function doesn't get triggered at all, after cell selection in the first tableView only prepare function triggers and It passes an empty value to the next view.
you override didDeselect instead of didSelectRowAt.
And you probably set a segue for tapping on cell.
Disconnect it and connect segue from your whole viewController.
Maybe you set your UITableView's selection styles to non-clickable.
Make UITableView -> Selection -> Single Selection and UITableViewCell -> Selection -> Default or something except the None
Make sure that you have set the delegate properly

How to use UITableView for TextField input view

I currently have an app which uses UIPickerViews to allow users to select which answer they want for a text field (to avoid spelling mistakes etc).
However, I have found that the UIPickerView isn't really what I want to use because I haven't had great feedback from it when testing.
I have done some research into how to use a UITableView for text field inputs instead, so when the user clicks the Textfield, the user segues to a UITableView with the same options which would be provided by the UIPickerView. Once they click the cell with the option they are looking for it would segue back to the form with the result chosen inside the text field. I thought this would be a better user experience as I could also implement the search to help users narrow down the option they require quicker.
I have been trying to get this to work for a while now, but I'm quite new at coding and haven't been able to crack it yet. I would just like advice on how to approach this? I'm using Swift and the Storyboard to create my app.
Would I need to create a separate ViewController with a UITableView that loads the options and then move the value back to the form once the cell is clicked?
One approach would be to use a table view in separate view controller. lets call it choiceVC and pass data which text field was tapped.
Then send the data back to your form to show what user has selected.
Follow these steps
Detect user tap on text field and segue to choiceVC by implementing this delegate function of UITextField
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
//push your choiceVC and pass data
var textFeildTapped = ""
//note: chooseGameTextField should be text field's outlet
if textField == chooseGameTextField{
textFeildTapped = "games"
}else if textField == chooseActiviyTextField {
textFeildTapped = "activiy"
}
//first set identifier of your view controller by going to identity inspector and setting the value StoryBoard ID
if let controller = storyboard?.instantiateViewController(withIdentifier: "choiceVC") as? ProductDetailVc{
//pass which text field was tapped
controller.choicesToShow = textFeildTapped
navigationController?.pushViewController(controller, animated: true)
}
return true
}
Note: choiceVC should have a variable "choicesToShow" of type string
in viewdidload of choiceVC check the variable
if choicesToShow == "games" {
//populate your table view with games.
}else {
//check for other cases and proceed accordingly
//activiy, console etc
}
Implement didSelect delegate method of UITableView
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Pass data back to your form using **delegate pattern**. see link in notes below
}
Notes:
See screen shot of setting storyBoard ID
see how to implement table view if you dont know
https://stackoverflow.com/a/33234181/7698092
See how to pass data backward using delegate pattern
https://medium.com/#mayooresan/passing-data-between-viewcontrollers-via-delegate-protocols-4ecde4b167de
If you are looking for an alternative for picker view to select options you can use dropdown like controls Eg.
DropDown
RSSelectionMenu
I hope these libraries can solve your issues, best of luck
I hope this code work for you. it's wroking for me.
First view controller for textfiled form where you want to open tableview.for that use textfield Delegate.
First View Controller
func doSomething(text: UITextField, with data: String) {
text.text = data
}
func textFieldDidBeginEditing(_ textField: UITextField) {
let objGametableVC = self.storyboard?.instantiateViewController(withIdentifier: "GametableVC") as! GametableVC;
objGametableVC.delegate = self
objGametableVC.selectedTextField = textField
if textField == txtActivity{
objGametableVC.tblData.removeAll()
objGametableVC.tblData = ["act1","act2"]
}
else if textField == txtGameName{
objGametableVC.tblData.removeAll()
objGametableVC.tblData = ["gam1","game2"]
}
textField.resignFirstResponder()
self.navigationController?.pushViewController(objGametableVC, animated: true);
}
Second view Controller For tableview show and pass data from second to first controller
Second view Controller
var delegate: Delegate?
var tblData: [String] = [String]()
var selectedTextField:UITextField? = nil
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tblData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tblcell = tableView.dequeueReusableCell(withIdentifier: "gamelblCell", for: indexPath) as! gamelblCell
tblcell.lblName.text = tblData[indexPath.row]
return tblcell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tblcell = tableView.cellForRow(at: indexPath) as! gamelblCell
let data = tblcell.lblName.text
delegate?.doSomething(text: selectedTextField!, with: data!)
self.navigationController?.popViewController(animated: true)
}

Making a segue from tapping on a image view in a cell [duplicate]

This question already has answers here:
How to assign an action for UIImageView object in Swift
(12 answers)
Closed 5 years ago.
In my Xcode project I want to create a segue from tapping on an image in a cell in a tableview. I have search around for an answer and the most relevant one was to change the image view to a button. This solution unfortunately does not work with my project because I have the image view customized in a certain way I want for when a image is in it.
So now I am in desperate need of help. I have a button with text in another cell that performs the same segue I want. It gets the objectID that I put in that cell and appends it to a global variable so when the other view appears, it will take that objectID and do the necessary functions. I have copied the code for that button below:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as! Cell1
cell.textButton.layer.setValue(indexPath, forKey: "index")
return cell
}
#IBAction func textPressed(_ sender: Any) {
let index = (sender as AnyObject).layer.value(forKey: "index") as! IndexPath
let cell = tableView.cellForRow(at: index) as! Cell1
globalText.append(cell.objectID)
let text = self.storyboard?.instantiateViewController(withIdentifier: "TextVC") as! TextVC
self.navigationController?.pushViewController(text, animated: true)
}
Now I just need to know how to do this exact same thing but for a imageview in a cell. Can someone please help?
Create a segue from your view controller to your destination and give it an "identifier" then
In cell for row
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(viewImage(_:)))
imageView.isUserInteractionEnabled = true
imageView.tag = tag!
imageView.addGestureRecognizer(gesture)
Create function to perform segue
func viewImage(_ sender: AnyObject) {
let tag = sender.view.tag
// do what ever with tag
performSegue(withIdentifier: "identifier", sender: self)
}

swift 3 Second TableView segue not showing in Navigation Controller

EDIT 2: Added a half-solution at bottom. Still open to a full solution
EDIT 1: Added images.
I need help fixing navigation between a second and third tableview.
I have a Navigation Controller, and three table views. The first two show up fine such that clicking a dynamic tableview cell > in the first tableview moves to the second tableview. However the second tableview cell chevron > while set in the storyboard (Accessory: Disclosure Indicator) doesn't show up during runtime, and therefore clicking on it does not move to the third tableview. I need help fixing that.
In each case I Ctrl+linked the cells to the next tableview in storyboard as show, and have prepare for segue in the previous tableview controllers. However only one works and the other seems orphaned.
If you could suggest a checklist in making sure the Navigation Controller works across three tableviews that would be helpful. Not sure what code to show so I'll post both segues. But it might not be a segue issue so I don't know.
first tableview Weeks works going to second tableview Leagues. Loads json file depending on what is clicked.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toLeagues"
{
let leaguesController = segue.destination as! LeaguesViewController
// pass the selected row
let selectedRow = self.tableView.indexPath(for: sender as! UITableViewCell)!.row
if selectedRow == 0
{
leaguesController.weekFileName = "sports_week_1"
}
else
{
leaguesController.weekFileName = "sports_week_2"
}
}
But second tableview Leagues doesn't work going to third tableview Games
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toGames"
{
let gamesController = segue.destination as! GamesViewController
// pass the selected row
let selectedRow = self.tableView.indexPath(for: sender as! UITableViewCell)!.row
gamesController.gameName = selectedRow.description
}
}
EDIT 1: Added images:
Starts off fine in first tableview...
Stops here in second tableview, can't move to third tableview
EDIT 2: Half solution
I found a half solution. Menu item now opens new view, only that the chevron appears after the fact. didSelectRowAt adds the missing disclosure indicator directly and goes to the new view controller. Couldn't find a viewWillAppear with IndexPath so I opted for didSelectRowAt. Works when clicked at least. Just the disclosure indicator missing on initial load. How to load the accessoryType before the view runs?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.accessoryType = .disclosureIndicator
// force menu to move to GamesViewController
let myGamesView = self.storyboard!.instantiateViewController(withIdentifier: "gamesView")
self.navigationController?.pushViewController(myGamesView, animated: true)
}
Try setting accessoryType in cellForRowAt instead of didSelectRowAt.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier") as! YourCustomCell
//Your other code
cell.accessoryType = .disclosureIndicator
return cell
}

Passing variable (Int) to a View Control (Popover)

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.

Resources