In an IOS app, I want to display a UIMenuController when longpressing an UIView located in UITableViewCell.
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
myTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell==nil)
{
cell=(myTableViewCell*)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
myView=<initMyView>
[cell.contentView addSubview:myView];
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longTapGestureCapturedOnView:)];
longTap.minimumPressDuration=0.5f;
[myView addGestureRecognizer:longTap];
return cell;
}
- (void)longTapGestureCapturedOnView:(UITapGestureRecognizer *)gesture
{
items=[[NSMutableArray alloc]init];
if (gesture.state == UIGestureRecognizerStateBegan)
{
[items addObject:[[UIMenuItem alloc] initWithTitle:#"Copy" action:#selector(myCopy:)]];
[items addObject:[[UIMenuItem alloc] initWithTitle:#"Custom" action:#selector(myCustom:)]];
}
[[UIMenuController sharedMenuController] setMenuItems:items];
[[UIMenuController sharedMenuController] update];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
BOOL result = NO;
if(#selector(myCopy:) == action ||#selector(myCustom:) == action ) {
result = YES;
}
return result;
}
The longTapGestureCapturedOnView is called, as well as the canPerformAction which is returning YES in the 2 cases, but no menuitem appears on the screen. What could I have done wrong ?
You also need
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
return YES ;
}
//
also it's not right to add gestures in cellForRowAt as it's being called upon scroll & cellReusing so add it in awakeFormNib method of UITableViewCell custom class
Related
I found already some solution how to detect touch in my cell, but they are made for one section. I have more then 1(8 tbh). Following code are making touch for few cells instead of one. So question is, what the clear solution to my problem? Here the code:
- (CatalogItemTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CatalogItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
CatalogItemModel *currentItemModel;
if(isCatSelected) {
currentItemModel = [[self.itemsArray objectForKey:[[self.sectionArray objectAtIndex:selectedIndexPath] valueForKey:#"id"]] objectAtIndex:indexPath.row];
} else {
currentItemModel = [[self.itemsArray objectForKey:[[self.sectionArray objectAtIndex:indexPath.section] valueForKey:#"id"]] objectAtIndex:indexPath.row];
}
[cell setItemModel:currentItemModel];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(didTapFav:)];
[cell.overFavorite addGestureRecognizer:gestureRecognizer]; //overFavorite is UIView to detect touch
return cell;
}
Action func:
-(void)didTapFav:(UITapGestureRecognizer*)recognizer {
NSString *token = [ProfileManager getToken];
if(token.length > 0){
[[NSNotificationCenter defaultCenter] postNotificationName:#"addToFavorite" object:self];
} else {
[self showErrorWithTitle:#"Auth" andText:#"To add item in Favorite!"];
}
}
Function inside CatalogItemTableViewCell:
-(void)addToFavorite {
if(isFavorite) {
[ApiManager tryAddToFavItem:[NSString stringWithFormat:#"%ld", (long)self.currentModel.uid] :^{
//Some API request
}];
}
}
First of all add gestureRecognizer for your CollectionView
UIGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tap:)];
tapGesture.delegate = self;
[self.collectionView addGestureRecognizer:tapGesture];
then get the IndexPath or Cell you want to access in - (void)tap:(UITapGestureRecognizer* )sender;
if (sender.state == UIGestureRecognizerStateEnded) {
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[sender locationInView:self.collectionView]];
}
I have a custom cell with various IBOutlets, but on one button I want to add a UILongPressGestureRecognizer for long press gestures. Here is my code (btw outlets are connected correctly and the IBAction method of the button is called correctly):
MyCustomCell.h
#interface MyCustomCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIButton *myButton;
#property (strong, nonatomic) UILongPressGestureRecognizer *longPressGestureRecognizer;
#end
MyCustomCell.m
- (void)awakeFromNib
{
// Initialization code
self.longPressGestureRecognizer = nil;
}
MyViewController.m
#import MyCustomCell.h
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"MyCell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (!cell){
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPressGestures:)];
cell.longPressGestureRecognizer.minimumPressDuration = 1.0f;
cell.longPressGestureRecognizer.allowableMovement = 300.0f;
[cell.myButton addGestureRecognizer:cell.longPressGestureRecognizer];
}
- (void)handleLongPressGestures:(UIGestureRecognizer *)recognizer
{
if ([recognizer.view isKindOfClass:[UIButton class]]){
if (recognizer.state == UIGestureRecognizerStateBegan){
NSLog(#"Long press began");
} else if (recognizer.state = UIGestureRecognizerStateEnded){
NSLog(#"Long press ended");
}
}
}
The problem is handleLongPressGestures: method is never called.
the longPressGestureRecognizer should be a property on the controller and not the view(MyCustomCell). Move the property over to MyViewController and try again. My guess is something weird is happening when it queues and dequeues the MyCustomCell.
Objects(cells) for reuse should be lightweight. In this case, the longPressGestureRecognizer's target is the view controller and is nasty.
Try out this way
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"MyCell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (!cell){
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
if ([[cell gestureRecognizers] count]<1) {
UILongPressGestureRecognizer *longPressGestureRecognizer;
longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPressGestures:)];
longPressGestureRecognizer.minimumPressDuration = 1.0f;
longPressGestureRecognizer.allowableMovement = 300.0f;
longPressGestureRecognizer.delegate = self;
[cell.myButton addGestureRecognizer:cell.longPressGestureRecognizer];
}
}
This type of code works for me.
Try This!
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILongPressGestureRecognizer *LongPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(handleLongPressGestures:)];
LongPress.minimumPressDuration = 1.0f;
LongPress.allowableMovement = 300.0f;
[cell.myButton addGestureRecognizer:LongPress];
}
- (void)handleLongPressGestures:(UIGestureRecognizer *)recognizer
{
if ([recognizer.view isKindOfClass:[UIButton class]]){
if (recognizer.state == UIGestureRecognizerStateBegan){
NSLog(#"Long press began");
}
else if (recognizer.state = UIGestureRecognizerStateEnded)
{
NSLog(#"Long press ended");
}
}
}
This question already has answers here:
Select UITableView's row when clicking on UISwitch
(7 answers)
Closed 8 years ago.
I have a table of dynamic content. In the prototype cell I have a button. In that button's action method, how do I then determine which row's button was pressed?
(I know that, for example, in a segue method one can determine which row has been pressed by querying the selected path of the table view, but in this case, no row has actually been selected.)
There are of course ways of doing this using tags, or storing an indexPath in your cell etc.
I prefer to use something similar to the following, which I think is cleaner than the above. You could add this to a category UITableViewController.
- (NSIndexPath *)indexPathForCellSubview:(UIView *)subview
{
if (subview) {
UITableViewCell *cell = [self tableViewCellForCellSubview:subview];
return [self.tableView indexPathForCell:cell];
}
return nil;
}
- (UITableViewCell *)tableViewCellForCellSubview:(UIView *)subview
{
if (subview) {
UIView *superView = subview.superview;
while (true) {
if (superView) {
if ([superView isKindOfClass:[UITableViewCell class]]) {
return (UITableViewCell *)superView;
}
superView = [superView superview];
} else {
return nil;
}
}
} else {
return nil;
}
}
Edit with suggestion from #staticVoidMan:
More concise implementation:
- (UITableViewCell *)tableViewCellForCellSubview:(UIView *)subview
{
UIView *checkSuperview = subview.superview;
while (checkSuperview) {
if ([checkSuperview isKindOfClass:[UITableViewCell class]]) {
break;
} else {
checkSuperview = checkSuperview.superview;
}
}
return (UITableViewCell *)checkSuperview;
}
Best approach:
-(void)buttonMethod:(UIButton *)sender event:(UIEvent *)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint pointCurrent = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForItemAtPoint:pointCurrent];
//...
}
PS: Provided you have done something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//...
[cell.someButton addTarget:self
action:#selector(buttonMethod:event:)
forControlEvents:UIControlEventTouchUpInside];
//...
}
You could use tags, associated objects, or superview trickery.
Here's a question that has all three answers.
Tags
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MySpecialCell";
MySpecialCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[MySpecialCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.mySpecialTextView.delegate = self;
}
cell.mySpecialTextView.tag = indexPath.row;
return cell;
}
- (void)textViewDidChange:(UITextView *)textView
int rowOfTextViewThatJustChanged = textView.tag;
}
Associated Objects
static NSString *const kIndexPathKey = #"indexPathKey";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MultiSelectCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UISwitch *switchView = [[UISwitch alloc] init];
[switchView addTarget:self action:#selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchView;
}
UISwitch *switchView = (id)cell.accessoryView;
[self setIndexPath:indexPath onSwitch:switchView];
switchView.on = [self isIndexPathSelected:indexPath];
id item = [self itemAtIndexPath:indexPath];
cell.textLabel.text = safePerformSelector(item, self.itemDescriptionSelector);
return cell;
}
- (void)switchValueChanged:(UISwitch *)sender {
NSIndexPath *indexPath = [self indexPathOfSwitch:sender];
[self setRowSelected:sender.isOn atIndexPath:indexPath];
[self.delegate didSelectItem:[self itemAtIndexPath:indexPath] atIndexPath:indexPath selected:sender.isOn sender:self];
}
- (void)setIndexPath:(NSIndexPath *)indexPath onSwitch:(UISwitch *)switchView {
[switchView setAssociatedObject:indexPath forKey:kIndexPathKey];
}
- (NSIndexPath *)indexPathOfSwitch:(UISwitch *)switchView {
return [switchView associatedObjectForKey:kIndexPathKey];
}
#implementation NSObject (AssociatedObjects)
- (void)setAssociatedObject:(id)object forKey:(NSString *const)key {
objc_setAssociatedObject(self, (__bridge const void *)(key), object, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)associatedObjectForKey:(NSString *const)key {
return objc_getAssociatedObject(self, (__bridge const void *)(key));
}
#end
I have a UITableView with custom cell TaskCell. TaskCell has checkboxImageView and I want that when user clicks on the checkboxImageView a method is fired. For some reason it is always firing the didSelectTableView delegate method. Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TaskCell";
Task *task = [tasks objectAtIndex:[indexPath row]];
TaskCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TaskCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell bindTo:task];
return cell;
}
TaskCell.m:
-(void) prepareGestureRecognizers
{
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onSingleTap:)];
singleTapGestureRecognizer.delegate = self;
singleTapGestureRecognizer.numberOfTapsRequired = 1;
self.checkboxImageView.userInteractionEnabled = YES;
[self.checkboxImageView addGestureRecognizer:singleTapGestureRecognizer];
}
-(void) onSingleTap:(UITapGestureRecognizer *) sender
{
NSLog(#"single tap!");
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) bindTo:(Task *)task
{
[self prepareGestureRecognizers];
self.titleLabel.text = task.title;
}
If you add the gesture recognizer in your TaskCell.m, then the following line
singleTapGestureRecognizer.delegate = self;
sets the delegate of the recognizer to the cell, not your controller. You should add the recognizer in your controller file so that the delegate is properly set up.
I'm not sure if this will work as I haven't tried myself, but you can try set the recognizer up in your Storyboard and let it be a #property of your cell. Then you can set the delegate to be your controller in your controller file.
I have a ballon view and when I tap on cell UIMenuController doesn't appear. I tried to implement in
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath,
then I added a long press gesture
(void)longPress:(UILongPressGestureRecognizer *)recognizer,
in the end I added an implementation in extended
UITableViewCell
class, but it still doesn't work.
Here is an example
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL) canPerformAction:(SEL)selector withSender:(id) sender
{
return YES;
}
-(void)copyAction:(id)sender{
NSLog(#"copy");
}
-(void)deleteAction:(id)sender{
NSLog(#"delete");
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
BalloonCell *cell = (BalloonCell*)[tableView cellForRowAtIndexPath:indexPath];
[self becomeFirstResponder];
BOOL isCanBecomde = [self canBecomeFirstResponder];
BOOL isFirst = [self isFirstResponder];
UIMenuItem *copy = [[UIMenuItem alloc] initWithTitle:#"Копировать" action:#selector(copyAction:)];
UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:#"Удалить" action:#selector(deleteAction:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
CGRect drawRect = [cell convertRect:cell.bounds toView:self.view];
[menu setTargetRect:drawRect inView:window];
[menu setMenuItems:[NSArray arrayWithObjects:copy, delete, nil]];
[menu setMenuVisible:YES animated:YES];
}
Do you have any guess why UIMenuController doesn't appear?