So I have a custom cell class and a xib file for my cells in my tableview, and I want to add a long press recognition to the custom cell.
I tried to drag and drop the long press recognizer to the cell in the xib-file, make the file's owner the gesture's delegate, and then "drag and add" it from the document outline to the custom class swift-file. I also added the UIGestureRecognizerDelegate to the custom cell class.
Now when I run the app I get a Thread 1: signal SIGABRT error, and honestly I have no idea why. This is the first time I use the gesture recognizer so sorry if there's an obvious mistake or something I should've done.
Any suggestions on how to proceed would be appreciated.
EDIT: As Logan suggested I undid everything that was done and followed his steps. The code in my custom class now looks like this:
#IBAction func pressed(sender: AnyObject) {
func handleLongPress(sender: UILongPressGestureRecognizer) {
if sender.state == .Began {
println("Received longPress!")
}
}
}
But I still get the error. What I noticed is that if I just add the gesture to the xib-file and run the app, I get the error. I don't know if that is because I haven't connected it to the custom class file yet, but I thought I'd just throw it out there.
EDIT 2: After changing my code to this:
#IBAction func pressed(sender: UILongPressGestureRecognizer) {
if sender.state == .Began {
println("Received longPress!")
}
}
I still get the error.
I don't know if I made this clear, but I can't even load the screen, it just crashes right away. So I'm thinking this is a delegation/reference issue, here's how my references are:
1) I set the class of my xib-file owner to the custom cell class.
2) I dragged the gesture recognizer on the cell.
3) I dragged the gesture object to the file-owner so that the file owner is its delegate.
4) I added the code from above in my custom class.
5) I dragged the gesture object to the file-owner so that the gesture is "connected to the code" in the custom class.
These are all my steps, and as I said, I suspect there's a delegate issue somewhere.
I had a similar issue. The problem is that when you add a gesture recognizer to the xib file, there are multiple objects (your tableview cell and the gesture recognizer) and the tableview datasource cannot handle this. The error describes the problem correctly. I moved my tableview cell to the main storyboard instead of keeping it in a separate xib file to resolve this issue.
reason: 'invalid nib registered for identifier (CELL) - nib must contain exactly one top level object which must be a UITableViewCell instance'
Another solution to your problem would be to create the gesture recognizer in code like:
self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tapFired:)];
in the awakeFromNib for the custom table view cell.
I just think the basic issue is that you cannot add gesture recognizers in a table view cell xib and then have the tableview data source load the cell.
1) I set the class of my xib-file owner to the custom cell class.
You are wrong at the very beginning. A UITableViewCell is a subclass of UIView. So you should set View to the custom cell class, not the file's owner. The file's owner should be NSObject.
You can only set file's owner as your custom class if it is the subclass of UIViewController.
Related
I've been trying to create table view cells each with a UIImageView serving as a background for them. However, when I tap on each cell the table view will not register the click and will not transition to the view controller I have hooked up, even while I'm using the didSelectRowAtIndexPath function.
I realize it's probably an issue with the ImageView obstructing the table views ability to register the cell tap. The cells will transition however when I drag my finger/mouse on it from left to right, just not on taps. I tried to use a Tap Gesture Recognizer on the Image View however it only worked for the cell at the very top and not all of them. How do I tackle this issue?
Here is an image of the table view cells so you have an idea of what I'm working with: http://imgur.com/a/Ku4uD. Thank you!
If you uncheck User Interaction Enabled on your Image View, the problem should be solved. When running into a problem always check the user interaction of the most child view and work your way up.
One trick I have also learned is to create a subclass of a child and override touchesShouldCancel like so:
override func touchesShouldCancel(in view: UIView) -> Bool {
print("touchesShouldCancel")
//Run extra code that you want when the user selects this view
//then still retrieve the tap by its parent.
return false
}
I am unsure of exactly what your problem is, but I would delete whatever segue that you have, add a new one by dragging from the yellow circle on the left side of the center portion of the top of your tableView ViewController inside the storyboard, to the viewController that you desire it to segue to. Give the segue an appropriate identifier, and then inside your tableView class under tableView didSelectRow add performSegue(withIdentifier: "ChosenIdentifier", sender: indexPath)
Then in prepare forSegue add in:
if let vc = sender.destination as? TheViewControllerYouAreSegueingTo {
if let indexPath = sender as? IndexPath {
vc.variableIdentifyingWhatCellWasClicked = indexPath.row
}
}
with whatever adjustment is needed to meet your specific needs.
I'm having a TableView. In that tableView, I custom it's cells
like this
In this cell I have 2 part. An Image and an TextField.
It's look like facebook's post status. When I click to Image, I want to open a new ViewController and the same for TextField
But I have a problem that I can't call segue from my cell's class file.
Are there any way that I can call a function in TableViewController's class from TableViewCell's class ?
I know that I can use delegate but I don't like this way a lot, because if I do this way I have to set a lot of delegate in my project.
Are there any better way to present a new ViewController directly from TableViewCell's class
Using a delegate is a very good way to solve your problem.
If you don't like that solution then you could design a table view cell that has a closure property set by the view controller, and invokes that closure when the button is pressed.
EDIT:
Another alternative is to leave the button action empty in your custom cell, but still connect up outlets to the buttons.
Then in your cellForRow(at:) method, install an IBAction that has your view controller as the target. (Credit to #SoanSani, who's answer is below)
In that IBAction method, you can use the coordinates of the button to figure out the indexPath of the cell hat contains the button that was tapped.
I've created an extension to UITableView that lets you figure out the cell that contains any view. The code is below:
import UIKit
public extension UITableView {
/**
This method returns the indexPath of the cell that contains the specified view
- Parameter view: The view to find.
- Returns: The indexPath of the cell containing the view, or nil if it can't be found
*/
func indexPathForView(_ view: UIView) -> IndexPath? {
let origin = view.bounds.origin
let viewOrigin = self.convert(origin, from: view)
let indexPath = self.indexPathForRow(at: viewOrigin)
return indexPath
}
}
You can use delegates, but if you dont want to use delegates
while setting up the cell add target to your button tableViewController class and then you can receive all events in tableview class rather than cell class.
But this would require to add tags which you can use to differentiate which button in cell was clicked
Objective C Code
[OkBtn addTarget:self action:#selector(okButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
Swift Code
OkBtn.addTarget(self, action: #selector(ViewController.okButtonTapped), for: .TouchUpInside)
I have a UITablaView in my Objective C application. I have the custom cells with a label and a UIImageView. I want to disable a part of the rows, to disable didSelectRowAtIndexPath when users click on this row's part.
I want this:
Is it possible?
Here is the simple and the most elegant solution that I can think off.
I believe you must be having a CustomCell, which holds the IBOutlet to imageView on the left side :) You can make use of hitTest method to solve your problem :)
In your CustomCell lets assume it to be MyTestCell class write this,
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if self.myImageView.frame.contains(point) {
return nil
}
return super.hitTest(point, with: event)
}
Where myImageView is the IBOulet of imageView on the left hand side of your cell :)
All am doing is checking where did user tap, if the touch point is within the frame of cells imageView you return nil as the view. When you return nil touch event stops propagating to its parent views and finally will never reach cell hence didSelectRowAtIndexPath never called,
On the other hand you should handover touch to the next view and you do it simply by calling same method on its super iOS will eventually trace it back to cell and triggers didSelectRowAtIndexPath
Hope it helps :)
You can do a simple trick (without the need of writing code) to solve this issue, by adding a button that covers the part that you want to disable the selection of it. Obviously, the button should not have any text or background color (it should be clear color), also make sure to add suitable constraints for making sure that is covered the wanted part of the cell.
So when tapping on the button, nothing should happen and didselectRow should not get called because the actual touching event should be referred to the button, not to the row.
Hope this helped.
This is very simple trick,
Add a button on profile image part, means on red part as shown in picture (provided by you). And don't do on click on button click.
Happy Coding!!!!
Simply put the button over Green area and set tag for each button. Onclick you can perform your functionality using tag. Like this
inside
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let obj: AnyObject = self.dataList.objectAtIndex(indexPath.row);
cell?.populateCellWithData(obj, indexPath: indexPath)
cell?.destinationLabel.userInteractionEnabled = true
let destRecognizer : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DestLabelTapped:")
destRecognizer.numberOfTapsRequired = 1
destRecognizer.delegate = self
cell?.destinationLabel.addGestureRecognizer(destRecognizer)
}
and in DestLabelTapped you can peform your functionlity
Another way
Just set two Tableview and scroll them side by side and one table is selectable and another is not. (don't do this)
In my understanding the tricks with buttons and other UIElements that are covering the content are not the right way to solve the target. As you will need extra manipulations with them in Storyboard, if you will need to make dynamically content, if you will work with constraints and many more situations where you will need to control your content and + the artificial cover. There are few things to do:
Set UITableView selection to No Selection
Put the second part that you want to be active in UIView. This UIView will be the content container.
Add UITapGestureRecognizer to UIView
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(selectCellAction:)];
Add IBAction where you could do all you need.
- (IBAction)selectCellAction:(id)sender
{
// do what you need
}
And thats all.
You may simply do change the image view width constraints to 0. Where you want to action is performed. As for example:
1) If you use a button that cover the image.
2) If you take a navigation bar then you there take a button and perform the action.
hope this will help.
I have an image and in the storyboard I've dragged a Tap Gesture Recognizer on top of this image and changed the settings:
For my TGR:
For my image:
How do I hook this up to my controller now? I want a method to fire off when the double tap happens. Is there some sort of protocol I have to conform to? Do people normally do this from the storyboard or programmatically in viewDidLoad? I don't mind doing it another way if that's the general trend of things.
connect the gesture recognizer as a outlet to your ViewController and in your viewDidLoad :
[self.yourGesture addTarget:self action:#selector(didTapOnImage:)] ;
and then declare your method didTapOnImage method :
-(void)didTapOnImage:(UIGestureRecognizer*) recognizer
{
//do your work here
}
I have some problems with using a UITableView on a UIView which haves a UITapGestureRecognizer.
If I click on a UITableViewCell, the background does not change to the selected one (because no touch is received).
I was in the assumption that if you add a childview to a parentview, the childview would be able to handle the touch and that everything would still work. However, this is not the cas. If I tap on a UITableviewCell, the tap is handled by this parentview and the cell is not selected.
I already tried to subclass "UITapGestureRecognizer" to ignore touch events from UITableViewCell:
(monotouch code)
public override bool ShouldReceiveTouch (UIGestureRecognizer recognizer, UITouch touch)
{
if (touch.View.GetType () == typeof(UITableViewCell))
return false;
return true;
}
It's not working because of I click on a cell, "touch.View.GetType ()" returns "UIView" and it's ignored.
Any idea's how I can make my tableview working on a UIView with a UITapGestureRecognizer?
If I remove the "UITapGestureRecognizer" from the parentview, the tableview acts normal.
Thanks Matt
When you create your UITapGestureRecogizer set the setCancelsTouchesInView property to NO. That should allow your UITableViewCell to receive the touch.