I have a table view with a few different prototype cells. I want to have one prototype cell segued to one controller and the rest should not be clickable i.e. no seg.
Right now, I have one button linked to a controller successfully but when I tap on the tableview prototype cell which is segued to a different controller (in the storyboard) nothing happens. I don't get any error either.
What is the best way to go around this? Can I make two different segues in the storyboard? Or do I need to implement something in the tableview method did select row at index path, somehow grab the class associated with the row clicked and programmatically segue to a different screen? Something like:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("here")
//self.performSegueWithIdentifier("profileSeg", sender: self)
}
I am not getting the println here when I select a cell and yes I have assigned the delegate in viewdidload.
EDIT
I've just realised I made a mistake when explaining the question. I actually have a view at the bottom which links to one view controller. It is not a tableview row... This is the seg that works. But I have ctrl dragged to from one type of cell to a second controlleR and that seg is not working. There are also 3 other types of cells none of which have segs attached. Is this anything to do with the issue?
If didSelectRowAtIndexPath not working, it can be caused by either your UITableViewDelegate has not been set, or, it might possible that Selection of your UITableView has been turned off, need to Turn on, see image to find how.
Cheers.
Don't segue directly from cell to 'ViewController', you must segue cell 'ViewController' to the other 'ViewController'. You can segue many times from 'ViewController'.
And please add this code
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
if indexPath.row == 0{
performSegueWithIdentifier("profileSeg", sender: self)
}else{
performSegueWithIdentifier("anySeg", sender: self)
}
}
There are two ways to do what you want:
First if you only want to link ONE TVCell with one ViewController you could do what you started to do: Link every TVCell with the ViewController you want to go from there. For that hold ctrl and drag and drop from the prototype to the ViewController. So every TVCellPrototype can have one segue and if you hit the cell it will automatically perform the segue.
This is also the reason why your code don't prints "profile seg".
In this case you don't need to give the segue an identifier.
The second way is better if you need to have MULTIPLE segues from one TVCell to 2 or more ViewControllers. For this you link the ViewController of the TVCells with the ViewControllers you want to go. That way you can set as many segues as you want but you have to give them an identifier because you have to call them in the tableView(... didSelectRowAtIndexPath) function. (click on the segue in interface builder to give it an identifier). This will look like:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("ProfileSeg", sender: AnyObject?())
}
Now lets talk about why you don't get the println("here"):
There has to be a mistake according the function because there is no "override" in front of it. In general the compiler won't build your App because it is missing. I don't have all the code so i can't exactly say whats your mistake.
Related
I am developing an iOS application in Swift.
I have create a UICollectionViewController. I have filled the UICollectionViewCells with images. How can I connect the cells individual with the different ViewController for example if i tap the first cell they will show the next ViewController (but the new ViewController need no data from the cell the content is completely different. I will only that they open the next view controller and this for every image in the UiCollectionView.
With prepareforsegue I have the problem that when I tap on every image that the same ViewController is showing. I want that every image open a different new ViewController. Which function does I need
Segues define the viewController they are transitioning to. If each cell needs to go to a different viewController, then you need a segue for each one.
Wire your segues from the viewController icon at the top of your UICollectionViewController and not from the prototype cell:
Give each of these segues a unique identifier in the Attributes Inspector:
In collectionView(_:didSelectItemAtIndexPath:), select the correct segue identifier using indexPath and call performSegueWithIdentifier(_:sender:):
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let segues = ["goToVC1", "goToVC2", "goToVC3"]
let segueID = segues[indexPath.item]
performSegueWithIdentifier(segueID, sender: self)
}
I have a TableView with 4 static cells, and each cell has a disclosure indicator. The disclosure indicator should fire a segue. This works fine, but only for the first cell. I cannot figure out how to wire up a segue for anything but the first cell, either programmatically or via the story board.
You can use the UITableView delegate like this:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// row 1 corresponds to the second cell.
if indexPath.row == 1 {
performSegueWithIdentifier("MySegueID", sender: self)
}
}
In the storyboard, create the segue from the first view controller (not from the cell itself) to the second view controller, and set its identifier to MySegueID``.
I need to add a table view in a specific are of my screen which is inside another UIView. When the user press a button the table view is created and displayed perfectly fine. However I need to know the user selection so I create it in the following way:
self.dynamicTableView = UITableView(frame: self.viewForTableView.bounds)
self.dynamicTableView?.dataSource = self
self.dynamicTableView?.delegate = self
self.messageZone.addSubview(self.dynamicTableView!)
self.dynamicTableView?.reloadData()
The data sources methods are being called but the delegate is not, so the following println is not working
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("Hello")
}
As additional information I'm using a custom table view cell
UPDATE: I just discover that if I implement shouldHighlightRowAtIndexPath sometimes if I do a long press it works, like if I keep the cell pressed for about four seconds, but not always, just some times. Can it be any view that is behind the table view what is causing this weird behaviour?
I have a uiviewcontroller with a container on it, embedded inside of that is a uitableviewcontroller that has static cells and static content. When I tap the cell "Trip Info" it segues to the Trip Info View Controller. When coming back the static cell is highlighted.
I have seen many posts saying to add code to the didselectrow tableview method but I dont use one because my content is static. Any ideas?
Okay, just because you have static content doesn't mean you get to skip using the tableview delegate. It's not hard to set up.
tableView.deselectRowAtIndexPath(index)
There's not another way.
I ended up figuring it out on my own.
You need to make a swift file for that UITableViewController that is embedded into the UIViewController.
Then make sure your tableview is a delegate, and then call the
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
in that new file you created.
I am making an iOS app that relies on a table view. In each cell of the table view, there are 4 buttons aligned on the bottom. I have a cell class that is pretty standard and a feedController to handle the table and setting all the items of the cell.
Everything works fine on it but I can not figure out how to handle the button clicks within the cell. I can hard code it into my cell class, but then every 3 cells has the same interaction. Is there a way to pass the button click function from the cell class into the controller? I have tried checking the state from the controller and that has not worked.
Can you add a gesture recognizer as you're doing your cellForItemAtIndexPath? So I had something similar with a collection view, and what I did was as it within:
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as MyCollectionView
...
I would add a gesture recognizer to each cell
i.e.
cell.addGestureRecognizer(UITapGestureRecognizer(target: self, action:Selector("tapAction:")))
And then something like:
func tapAction(recognizer: UITapGestureRecognizer) {
...
}
so recognizer ends up being the specific item tapped, and I could take action accordingly (in my case, I had my datasource of items and I would find the item in an array by casting recognizer to a cell, finding the appropriate subview, and update values on it)
I would add code block properties to your cell class which the table can assign to deal with each button. In your cell, code each button handler to call the appropriate block, or pass an index for the button used in a single block.
See my answer here which has an example, but for a switch.
How can I get index path of cell on switch change event in section based table view
If after a few cells you get the same interaction, it's possibly because you're dequeueing a reusable cell, and you're getting the same cell.
Make sure to set your .setTarget() call for your buttons in your tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) data source every time the cell is dequeued. It would help if you shared how you're handling dequeuing to see if this is your issue.