Reputation: 65
I have a feeling the answer is going to be "you can't do that", but hope springs eternal!
One scene in my app is a UITableView; the table represents a month and each row is a day of the month. I have a small UIView at the top of the table with a "Prev" and 'Next" button. Selecting next results in the table displaying the next month and Prev causes the prior month to be displayed. All of this works fine.
What I would like to do is add left & right gesture recognizers to move from month to month, performing the same function as the Prev and Next buttons. Please note I don't need anything to happen to/with the UITableViewCell.
When I searched for this on the web, the overwhelming number of posts seems to be how to handle gesture recognizers in a UITableViewCell. This is not helpful in my case.
I am using Objective-C and Xcode 11.3.1.
Any ideas?
Thanks in advance.
Upvotes: 1
Views: 1014
Reputation: 65
The issue is fixed.
I found that the recognizer worked as I expected if I added the swipe recognizer to my tableview programmatically. But if use the storyboard to add the swipe recognizers, they did not work at all. This is what i was testing when I posted my question.
After digging a bit, I found that the storyboard recognizers needed to be added to the reference outlet collections in my tableview. For whatever reason this did not happen when I added them originally. Once I connected the recognizers to my tableview, everything worked exactly as intended.
Upvotes: 0
Reputation: 379
Answer in Objective C
same answer like @Alan but just no need to add below line in cellForRowAt
method
cell.isUserInteractionEnabled = false
cell.backgroundColor = .random()
Use below method for swipe gesture in TableView
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UISwipeGestureRecognizer *leftGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftHandler)];
[self.tableView addGestureRecognizer: leftGestureRecognizer];
UISwipeGestureRecognizer *rightGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightHandler)];
[self.tableView addGestureRecognizer: leftGestureRecognizer];
}
-(void) swipeLeftHandler
{
NSLog(@"SWIPE Left");
// add same method which you call on next button’s click
}
-(void) swipeRightHandler{
NSLog(@"SWIPE Right");
// add same method which you call on previous button’s click
}
Upvotes: 0
Reputation: 1142
You should be able to do it. By disabling the interaction of each cell you should be able to pass a 'UIGestureRecognizer' to the tableView.
Here is a short example. Sorry I used Swift but you can probably get the gist of it.
After adding the tableView I Added these gestureRecognizers to it.
let leftGesture = UISwipeGestureRecognizer(target: self, action: #selector(leftSwiped))
leftGesture.direction = .left
tableView.addGestureRecognizer(leftGesture)
let rightGesture = UISwipeGestureRecognizer(target: self, action: #selector(rightSwiped))
rightGesture.direction = .right
tableView.addGestureRecognizer(rightGesture)
Each one calls the following functions:
@objc func leftSwiped() {
print("LEFT")
}
@objc func rightSwiped() {
print("Right")
}
and in cell for row I just disabled the user interaction of the cell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.isUserInteractionEnabled = false
cell.backgroundColor = .random()
return cell
}
When I tried it, swiping right and left on the table view would print appropriately in the logger.
Hope this helps, Alan
Upvotes: 1