Accesing parent cell labels swift - ios

I have a button placed inside a TableViewCell, the button triggers a action which passes data to another ViewController and displays it. Is there any way to access the labels of the particular cell that I clicked?
The cells are being created and populated from an API.
For example, I have 10 cells, all having different labels inside. I need to access the labels of the cell that I clicked (Or the cell that the button I clicked on is a child to) and pass this data to my other ViewController
Cheers!

I solved this by giving the button tag an ID from my API and passing this to the other ViewController.

That works, I had just figured this out:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
let indexPath = self.tableView.indexPathForSelectedRow()
let cell = self.historyViewTable.cellForRowAtIndexPath(indexPath!)
println(cell.cellLabel.text!)
}

Related

go back to a certain viewController

my Storyboard looks like:
User can go directly from the 1st screen to the 4th. The fourth screen contains tableView, with XIB cell design. I want user to be able to tap on a cell and get to the 3rd screen and send some data with that. I know this should be done in didSelectRowAt. But is it even possible?
Yes it is possible.
Create a segue. In your storyboard, ctrl-drag from the first button on top of your 4th screen to anywhere on your second screen. You should see a new segue created in your storyboard.
Give the segue an id. Click on the newly created segue, in the right panel, set the indentifier attribute under the Indentity Inspector tab.
Perform the segue. In your didSelectRowAt, add the following line:
self.performSegue(withIdentifier: "THE_ID_YOU_ASSIGNED_IN_STEP_2")
Send data to the destination segue. In your screen 4 view controller, add the following function:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "THE_ID_YOU_ASSIGNED_IN_STEP_2" {
if let destinationVC = segue.destination as? YOUR_SCREEN_3_VC {
// Send data to your VC, such as assigning values
}
}
}

How do I move from one ViewController (containing a custom UIView) to another ViewController (containing a UITableView) on click?

NOTE: I understand, similar questions have been asked many times here at stackoverflow, but I could not solve the problem I'm facing. So please don't mark this question as "already asked" or so.
Actually I've two ViewControllers, CalendarViewController and a FruitsListViewController.
The CalendarViewController contains MyCalendarView (subclass of UIView) to display the calendar, while FruitsListViewController contains UITableView to display the list of fruits.
When I click in the certain region of MyCalendarView within the CalendarViewController, it extracts the corresponding date and prints to the log.
Question:
I want to achieve that when I click on the certain place of the MyCalendarView of the CalendarViewController, I'm brought to the FruitsListViewController. But I'm not able to drag the segue from that MyCalendarView to the FruitsListViewController (in fact only able to drag from that MyCalendarView to its parent view and nothing elsewhere).
Kindly guide me how can I achieve this?
Regards.
Drag segue from first ViewController to the other ViewController.
on ta, call performSegueWithIdentifier("yourSegueIdentifier", sender: nil) and in prepareForSegue customize what data should be sent to the other ViewController.
Your should override prepareForSegue from CalendarViewController class and call the prepareForSegue(::) from some clickCertainPlaceOfTheCustomUIView()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destView = segue.destinationViewController as! FruitsListViewController
destView.chosenFruitsArray = self.myFruitsArray
}

How to Segue to a specific view controller depending on the Segue identity, from a Collection view cell

Currently developing a small ios app in swift, I have populated a collection view cell with data from a .plist, each cell has a title and button, what i'm wanting is to segue to multiple view controllers in the storyboard depending on the segue identity once the button is pressed. For example if the segue has the id set as food, i want it to navigate to the view called food?
or if it is easier for the segue to pull the title from the cell then navigate to the view with the same title as the cell?
ill try and explain in code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "food"
{
//then navigate to the view controller called food
}
else if segue.identifier == "drink"{
{
//navigate to the view called drink
}
}
If food, drink or any other items have their own unique view in storyboard this is achievable.
You can assign a string to each button in UICollectionView and check which item in the collection was tapped and do a performSegueWithIdentifier: with button string.
performSegueWithIdentifier("toComments", sender: self)
Then prepareForSegue: method to pass data. Do another if for drink.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toFood" {
let ExchangeViewData = segue.destinationViewController as! FoodViewController
ExchangeViewData.foodMenuToShow = foodMenuID //This can be anything that you get your food items.
}
}
You can't do what you are asking for. A segue identifier is what determines which view controller gets invoked by the segue. If you want to go to a different view controller, invoke a segue with a different identifier. By the time you get to prepareForSegue it's too late. The segue to a specific view controller is already in progress.
You should explain what it is you're trying to do and we can help you solve your problem rather than trying to fight the APIs and do something that's neither possible nor appropriate. (Your question is an example of an "XY problem".)

Swift - Can not detect button action in uitableviewcell after set auto layout

There is a button in my custom tableviewCell, when I press the UIButton, it will open another view in my app.
Everything is okay after I try to active the auto layout attribute.
And even I don't set any constraint to my button, it still can not be
detected and shift to another view.
A snapshot image here
Can anyone help me ?
UPDATE:
Thanks for Korpel, rose and Tobonaut.
Yes I had create the segue on the button to another view, and it worked before I set the attribute of autoLayout. I was sure I added the segue and identifier both on the storyboard and the code .
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier=="RemindSegue" {
let destination = segue.destinationViewController as! RemindTimeController
let senderButton = sender as? UIButton
let indexPath = NSIndexPath(forRow: (senderButton?.tag)!, inSection: 1)
destination.delegate = self
}
}
After I set the autoLayout option, I could only click on my custom tableCell, which was dynamically created on the runtime. And the click on the button didn't work anymore.
The button's still there, there was no constraint added to it.
Ok, I find the answer of it.
I added the following code in my custom tableViewCell when initiate...
which disabled my button to use.
But I don't really understand the reason behind it...
self.contentView.translatesAutoresizingMaskIntoConstraints = false

separate segue on section header as on table view row

I have a tableview setup whereby every row has its own custom section header. This is done with a prototype cell in the story board. I have a segue to a view controller on the click event of a row. I achieved this by ctrl dragging on the storyboard.
I want my section header to segue to a different controller, however. I ctrl dragged from my header prototype cell to a view controller on the storyboard also.
The result is that both clicking on the row and clicking on the header of the row are bringing me to the same view controller.
It seems the header is linked to the row. Is there any way around this? Could I give my segues identifiers and override the prepare for seg? Or is it possible to have a separate click event on a section header as the rest of the row?
UPDATE:
I have to tried to implement it as follows:
I ctrl dragged from my vc to the destination vc in the storyboard and gave the seg an identifier.
In my viewForHeaderInSection, I have added the following:
let tapper = UITapGestureRecognizer(target: self, action:Selector("seg"))
tapper.cancelsTouchesInView = false
headerCell.addGestureRecognizer(tapper)
return headerCell
my seg func just perform a seg with the identifier I set up as explained above.
func seg(){
self.performSegueWithIdentifier("feedToMembersDetailed", sender: self)
}
In my overridePrepareForSeg, I check the identifier and call the appropriate method:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "feedToMembersDetailed"){
//getting in here
getMembersContentFromRow(segue)
}
... extra conditions
}
and finally my getMembersContentFromRow looks as follows:
func getMembersContentFromRow(segue : UIStoryboardSegue){
var membersDetailed = segue.destinationViewController as! MembersDetailedController
if let indexPath = self.feedTableView.**indexPathForSelectedRow()**{
if let selectedId = self.newsFeed?[indexPath.section].authorId{
Prefs.followedId = selectedId.toInt()!
}
println("setting postAuthorPic as \(self.newsFeed?[indexPath.section].authorPic)")
membersDetailed.postAuthorPic = self.newsFeed?[indexPath.section].authorPic
membersDetailed.postAuthorID = self.newsFeed?[indexPath.section].authorId
membersDetailed.postAuthor = self.newsFeed?[indexPath.section].author
}
else{
//getting in here. I need a method that can retrieve indexPath for me
println("could not get selected row")
}
}
Ok here it is, I am not writing code, just the steps you need to follow
Remove the segues you have created from your row and headers. Just assign SegueIdentifier for the controllers to be opened, say SegueIdentifierForRow and SegueIdentifierForSection.
Since there is no action in tableview headers, you need to add a custom action may be an UIButton or, UITapGestureRecognizer. Cell selection you can leave as it is. And to determine section selection you can set tag for each section.
On Row CellSelection manually initialise ViewController by using identifier SegueIdentifierForRow, and same for section using different identifier SegueIdentifierForSection.
Follow above you should be able to do whats required.
Cheers.
You would need to implement viewForHeaderInSection: method and add a UITapGestureRecognizer to a view which would be retuned as header view. You can then handle the tap on your table section headers to go to different View Controller.

Resources