I am using ABMenuTableViewCell tableview controller in my application. I want to call didSelectRowAtIndexPath when i swipe a UITableViewCell.
Right now didSelectRowAtIndexPath only execute when I tap on a cell, I want to call it even when I swipe it. here is my didSelectRowAtIndexPath and cellforRowAtIndexPath methods code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *likes;
UILabel *downloads;
static NSString *CellIdentifier = #"Cell";
ABMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[ABMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"acc_arrow_back.png"]];
arrow.frame = CGRectMake(300, 50, 5, 12);
arrow.image = [arrow.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[arrow setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
[cell.contentView addSubview:arrow];
UIImageView *likes_img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"social.png"]];
likes_img.frame = CGRectMake(15, 80, 15, 15);
likes_img.image = [likes_img.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[likes_img setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
[cell.contentView addSubview:likes_img];
likes =[[UILabel alloc]initWithFrame:CGRectMake(33, 78, 80, 20)];
likes.tag = 1001; // set a tag for this View so you can get at it later
likes.textColor=[UIColor darkGrayColor];
likes.font=[UIFont fontWithName:#"Helvetica" size:10.0f];
likes.text=[[rssOutputData objectAtIndex:indexPath.row]xmllikes];
[cell.contentView addSubview:likes];
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
UIImageView *downloads_img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"download.png"]];
downloads_img.frame = CGRectMake(55, 79, 15, 15);
downloads_img.image = [downloads_img.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[downloads_img setTintColor:[UIColor colorWithRed:(191/255.0) green:(2/255.0) blue:(6/255.0) alpha:1]];
[cell.contentView addSubview:downloads_img];
downloads =[[UILabel alloc]initWithFrame:CGRectMake(73, 78, 80, 20)];
downloads.tag = 1002; // set a tag for this View so you can get at it later
downloads.textColor=[UIColor darkGrayColor];
downloads.font=[UIFont fontWithName:#"Helvetica" size:10.0f];
downloads.text=[[rssOutputData objectAtIndex:indexPath.row]xmldownloads];
[cell.contentView addSubview:downloads];
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
}
else
{
// use viewWithTag to find lblNombre in the re-usable cell.contentView
likes = (UILabel *)[cell.contentView viewWithTag:1001];
downloads = (UILabel *)[cell.contentView viewWithTag:1002];
}
cell.textLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmlsinger];
cell.detailTextLabel.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
// custom menu view
NSString *nibName = #"ABCellMailStyleMenuView";
ABCellMenuView *menuView = [ABCellMenuView initWithNib:nibName bundle:nil];
menuView.delegate = self;
menuView.indexPath = indexPath;
cell.rightMenuView = menuView;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
And these are the methods in cell class
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer and
- (void)swipeGesture:(UIPanGestureRecognizer *)gesture
here is when i swipe
You can modified swipeGesture method will following code. it will show UITableViewCell as selected once you perform swipe operation.
- (void)swipeGesture:(UIPanGestureRecognizer *)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
NSInteger direction;
// find swipe direction
CGPoint velocity = [gesture velocityInView:self];
if (velocity.x > 0) {
// towards right - hide menu view
direction = ABMenuUpdateHideAction;
}
else {
// towards left - show menu view
direction = ABMenuUpdateShowAction;
}
UITableView* tableView = (UITableView*)self.superview.superview;
CGPoint swipeLocation = [gesture locationInView:tableView];
NSIndexPath *swipedIndexPath = [tableView indexPathForRowAtPoint:swipeLocation];
[tableView selectRowAtIndexPath:swipedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
[self updateMenuView:direction animated:YES];
}
}
Hope this help you.
As you are trying to achieve behaviour not supported by ABMenuTableViewCell, you will need to edit it's source:
Add _tapGesture and _menuVisible instance variables:
#implementation ABMenuTableViewCell {
CGRect _rightMenuViewInitialFrame;
UIPanGestureRecognizer *_swipeGesture;
UITapGestureRecognizer *_tapGesture;
BOOL _menuVisible;
}
Implement -tapGesture: method:
- (void) tapGesture:(UITapGestureRecognizer*)gesture {
if (_menuVisible)
[self updateMenuView:ABMenuUpdateHideAction animated:YES];
else
[self updateMenuView:ABMenuUpdateShowAction animated:YES];
}
Add UITapGestureRecognizer inside -commonInit method:
- (void) commonInit {
_swipeGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGesture:)];
_swipeGesture.delegate = self;
[self addGestureRecognizer:_swipeGesture];
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:)];
_tapGesture.delegate = self;
[self addGestureRecognizer:_tapGesture];
}
Update _menuVisible inside -updateMenuView:animated::
- (void)updateMenuView:(ABMenuUpdateAction)action animated:(BOOL)animated {
...
switch (action) {
case ABMenuUpdateShowAction:
menuNewFrame = CGRectMake(CGRectGetWidth(self.contentView.frame) - initialWidth, .0, initialWidth, CGRectGetHeight(self.contentView.frame));
_menuVisible = YES;
break;
case ABMenuUpdateHideAction:
menuNewFrame = CGRectMake(CGRectGetWidth(self.contentView.frame), .0, .0, CGRectGetHeight(self.contentView.frame));
_menuVisible = NO;
break;
default:
break;
}
...
}
You won't be able to select cells, but as I understand it, you don't want to.
If you want the method to be called when swiping the cell, you should be using a UISwipeGestureRecognizer and not a UIPanGestureRecognizer.
In your viewDidLoad:, set up the gesture recognizer.
// Create a left swipe gesture recognizer
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeft:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
// Add it to the table view
[self.tableView addGestureRecognizer:recognizer];
Handle the swipe.
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
// Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:self.tableView];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
// Check if index path is valid
if (indexPath)
{
// Select the cell at the indexPath
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
That should call the tableView: didSelectRowAtIndexPath: method.
Related
I am attempting the following:
Within each cell I have date-from and date-to UITextField. When the user presses either TextField an InputView of the DatePicker appears with a Done button to complete.
My challenge is I only want the user to be able to select either of the TextFields inside the selected cell and no other. The user must press Done on InputView when they have finished, so the app can validate and reload TableView if necessary.
I have code that manages normal cell selection well when the 'keyboard' is shown, but cannot manage the UITextField behavior as I would like.
I share some code in the hope someone can help me:
- (void)keyboardWillShow:(NSNotification*)aNotification {
self.TableViewDiary.allowsSelection = NO;
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
self.TableViewDiary.allowsSelection = YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ActivityDiaryCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ActivityDiaryCellId"];
cell.activity = [self.activitycollection objectAtIndex:indexPath.row];
cell.LabelName.text = cell.activity.name;
cell.TextFieldStartDt.text = [self FormatPrettyTime :cell.activity.startdt];
cell.TextFieldEndDt.text = [self FormatPrettyTime :cell.activity.enddt];
cell.datePickerStart.date = cell.activity.startdt;
cell.datePickerEnd.date = cell.activity.enddt;
cell.tag = indexPath.row;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
ActivityDiaryCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.datePickerStart = [[UIDatePicker alloc] init];
self.datePickerStart.datePickerMode = UIDatePickerModeDateAndTime;
//self.datePickerStart.date = self.activity.startdt;
[self.datePickerStart addTarget:self action:#selector(datePickerStartValueChanged:) forControlEvents:UIControlEventValueChanged]; // method to respond to changes in the picker value
self.datePickerEnd = [[UIDatePicker alloc] init];
self.datePickerEnd.datePickerMode = UIDatePickerModeDateAndTime;
//self.datePickerEnd.date = self.activity.enddt;
[self.datePickerEnd addTarget:self action:#selector(datePickerEndValueChanged:) forControlEvents:UIControlEventValueChanged]; // method to respond to changes in the picker value
self.datePickerToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 44)];
[self.datePickerToolbar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(dismissPicker:)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self.datePickerToolbar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
self.TextFieldStartDt.delegate = self;
self.TextFieldStartDt.inputView = self.datePickerStart;
self.TextFieldStartDt.inputAccessoryView = self.datePickerToolbar;
self.TextFieldEndDt.delegate = self;
self.TextFieldEndDt.inputView = self.datePickerEnd;
self.TextFieldEndDt.inputAccessoryView = self.datePickerToolbar;
}
Keyboard is showing because a user has tapped a text field, not because they selected a cell. That's why allowsSelection doesn't affect it.
Move the UITextFieldDelegate handling to your UITableViewController. Then you can easily decide whether to show a date picker or not:
if date picker is hidden, show it and remember a cell which textfield is on
if date picker is already visible, check whether text field belongs to the same cell and either update date picker or do nothing (by overriding textFieldShouldBeginEditing(_:) and returning false)
I moved the UITextFieldDelegate up to the TableViewController and included following code there:
-(bool) textFieldShouldBeginEditing:(UITextField *)textField {
if (self.TableViewDiary.allowsSelection) {
return true;
} else {
CGPoint buttonPosition = [textField convertPoint:CGPointZero toView:self.TableViewDiary];
NSIndexPath *indexPath = [self.TableViewDiary indexPathForRowAtPoint:buttonPosition];
ActivityDiaryCell *cell = [self.TableViewDiary cellForRowAtIndexPath:indexPath];
if ([cell.TextFieldStartDt isFirstResponder] || [cell.TextFieldEndDt isFirstResponder]) {
return true;
}
}
return false;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGPoint buttonPosition = [textField convertPoint:CGPointZero toView:self.TableViewDiary];
NSIndexPath *indexPath = [self.TableViewDiary indexPathForRowAtPoint:buttonPosition];
ActivityDiaryCell *cell = [self.TableViewDiary cellForRowAtIndexPath:indexPath];
[cell setSelected:YES animated:NO];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGPoint buttonPosition = [textField convertPoint:CGPointZero toView:self.TableViewDiary];
NSIndexPath *indexPath = [self.TableViewDiary indexPathForRowAtPoint:buttonPosition];
ActivityDiaryCell *cell = [self.TableViewDiary cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:YES];
}
I need some help. Today, I am working on table view custom cell where the cell contains an UIImageView. On the ImageView, I want to implement the long gesture. I implement code for this that is give below.. But I am doing something wrong in my code.In this the View is resize once on long press but i want after the some seconds it can be remove and come back in table view cell
Can anyone Suggest me????
Update:
Here's the code!
- (void)celllongpressed:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
cell = (ActivityFeedCell *)[gesture view];
}
if (gesture.state == UIGestureRecognizerStateChanged)
{
cell = (ActivityFeedCell *)[gesture view];
logGes_view=[[UIView alloc]initWithFrame:CGRectMake(5, 0,self.view.frame.size.width-10,self.view.frame.size.height)];
image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 80,self.view.frame.size.width, self.view.frame.size.height-80)];
image.image=cell.updated_imgView.image;
UILabel *name_label=[[UILabel alloc]initWithFrame:CGRectMake(10, 15, 150, 30)];
//city_label.backgroundColor=[UIColor yellowColor];
name_label.text=lgGesNamelbl;
UILabel *city_label=[[UILabel alloc]initWithFrame:CGRectMake(10, 50, 180, 30)];
//city_label.backgroundColor=[UIColor yellowColor];
city_label.text=lgGesCitylbl;
[logGes_view addSubview:city_label];
[logGes_view addSubview:name_label];
[logGes_view addSubview:image];
logGes_view.backgroundColor=[UIColor whiteColor];
[self.view addSubview:logGes_view];
}
if (gesture.state == UIGestureRecognizerStateEnded)
{
// cell = (ActivityFeedCell *)[gesture view];
[logGes_view removeFromSuperview];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(celllongpressed:)];
[gesture1 setDelegate:self];
[gesture1 setMinimumPressDuration:1.0];
[ cell setUserInteractionEnabled:YES];
[cell addGestureRecognizer:gesture1];
}
UILongPressGestureRecognizer *reconizer=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(handleLongPress:)];
[reconizer setMinimumPressDuration:1.0];
[cell addGestureRecognizer:reconizer];
-(void)handleLongPress:(UILongPressGestureRecognizer*)reconizer
{
if (gesture.state == UIGestureRecognizerStateBegan)
{
UITableViewCell *cell = (UITableViewCell *)[gesture view];
NSIndexPath *indexPath = [tableview indexPathForCell:cell];
NSString *s = [NSString stringWithFormat: #"row=%1ld",(long)indexPath.row];
[self setTitle: s];
}
if (gesture.state == UIGestureRecognizerStateChanged)
{
cell = (UITableViewCell *)[gesture view];
cell.updated_imgView.frame=CGRectMake(0, 0, tableview.frame.size.width, tableview.frame.size.height);
}
if (gesture.state == UIGestureRecognizerStateEnded)
{
cell = (UITableViewCell *)[gesture view];
cell.updated_imgView.frame=CGRectMake(0, 0, 100, 100);
}
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}
[cell addGestureRecognizer:gesture1]
replace with below line
[ cell.yourimageview setUserInteractionEnabled:YES]; // This enable user interaction on the image view. Required!!
[cell.yourimageview addGestureRecognizer:gesture1]; //yourimageview is your image outlet
Try it:
Cell is superView of imageView so on size changing it will not cross cell frame
add it to mainView after resize.
I have a tableviewcontroller and a custom cell. What i wanna do is when i tap the cell, the cell is supposed to exapand and a view (graph view actually) is supposed to become subviewed inside the cell. Now the problem is that everything works fine but the graph is duplicated on some other cells as well.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ProductsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (cell == nil)
{
NSLog(#"empty cell");
}
//Product Label
cell.productNameLabel.text = #"something";
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
indexPathforChart = indexPath;
[self performSelector:#selector(addChart:) withObject:indexPath afterDelay:0.2];
[tableView beginUpdates];
[tableView endUpdates];
[tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
}
-(void)addChart:(NSIndexPath*)indexPath
{
BEMSimpleLineGraphView *myGraph = [[BEMSimpleLineGraphView alloc] initWithFrame:CGRectMake(0, 60, screenSize.width, 200)];
myGraph.dataSource = self;
myGraph.delegate = self;
myGraph.interpolateNullValues = YES;
myGraph.enableTouchReport = YES;
myGraph.tag = 100;
myGraph.animationGraphStyle = BEMLineAnimationDraw;
myGraph.enablePopUpReport = YES;
myGraph.enableXAxisLabel = YES;
myGraph.colorXaxisLabel = [UIColor darkGrayColor];
ProductsTableViewCell *cell = (ProductsTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:myGraph];
[cell setNeedsLayout];
[cell setNeedsDisplay];
myGraph.colorTop = [UIColor clearColor];
myGraph.colorBottom = [UIColor clearColor];
myGraph.colorLine = [UIColor darkGrayColor];
myGraph.colorPoint = [UIColor lightGrayColor];
}
This is caused by cell re-use.
ProductsTableViewCell *cell = (ProductsTableViewCell*)[self.tableView
cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:myGraph];
You added myGraph as a subview in the cell without removing it when the cell is re-used by some other index path while you scroll the table view.
The most appropriate way should be having a custom view inside the cell for drawing your graph, instead of adding/removing the graph view when needed. For the sake of scrolling performance, you may also cache the graph in case it will be used when user scrolls back and forth.
Cells are reused, so before loading a new cell you should implement the method prepareForReuse and add/remove or hidden/unhidden the views your cell requires.
So basically, ProductsTableViewCell should implement the method prepareForReuse. The easiest way to remove your BEMSimpleLineGraphView based on your code would be:
- (void) prepareForReuse{
UIView *v = [cell.contentView viewWithTag:100];
if ( v ) {
[v removeFromSuperView];
}
}
However, I don't consider using viewWithTag is the best solution so I would change the code into something similar to:
tableviewcontroller
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ProductsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
[cell addChart];
[tableView endUpdates];
[tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
}
ProductsTableViewCell
#interface DLSContactUsViewController ()
#property (strong,nonatomic) BEMSimpleLineGraphView *myGraph;
#end
-(void)addChart
{
if ( ![self.myGraph isDescendantOfView] ){
[self.contentView addSubview:self.myGraph];
[self setNeedsLayout];
[self setNeedsDisplay];
}
}
- (BEMSimpleLineGraphView*) myGraph{
if ( !_myGraph ) {
_myGraph = [[BEMSimpleLineGraphView alloc] initWithFrame:CGRectMake(0, 60, screenSize.width, 200)];
_myGraph.dataSource = self;
_myGraph.delegate = self;
_myGraph.interpolateNullValues = YES;
_myGraph.enableTouchReport = YES;
_myGraph.tag = 100;
_myGraph.animationGraphStyle = BEMLineAnimationDraw;
_myGraph.enablePopUpReport = YES;
_myGraph.enableXAxisLabel = YES;
_myGraph.colorXaxisLabel = [UIColor darkGrayColor];
_myGraph.colorTop = [UIColor clearColor];
_myGraph.colorBottom = [UIColor clearColor];
_myGraph.colorLine = [UIColor darkGrayColor];
_myGraph.colorPoint = [UIColor lightGrayColor];
}
return _myGraph;
}
- (void) prepareForReuse{
if ( [self.myGraph isDescendantOfView] && !self.isSelected ) {
[myGraph removeFromSuperView];
}
}
I have created the iPhone app which has the custom tableview cell and I also created a view in cellForRowAtIndexPath. Now, when i swiping the the cell in UITable the view that i created should be displayed in the particular cell and all other cell contents should be remained same.
My code as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RKCardCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"RKCardCell"];
// Configure the cell...
if (cell == nil) {
cell = [[RKCardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"RKCardCell"];
}
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeMethod:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:swipeGesture];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(-320.0, 8.0, 300, 180)];
[view setBackgroundColor:[UIColor greenColor]];
cell.profileImage.image = [UIImage imageNamed:[photoArray objectAtIndex:indexPath.row]];
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.nameLabel.text = [nameArray objectAtIndex:indexPath.row];
cell.descriptionLabel.text = [descriptionArray objectAtIndex:indexPath.row];
//%%% I made the cards pseudo dynamic, so I'm asking the cards to change their frames depending on the height of the cell
cell.cardView.frame = CGRectMake(10, 5, 300, [((NSNumber*)[cardSizeArray objectAtIndex:indexPath.row])intValue]-10);
return cell;
}
Following code is for gesture swiping:
- (void)swipeMethod:(UIGestureRecognizer *)gestureRec
{
[UIView animateWithDuration: 0.5 animations:^{
CGRectOffset(self.rkCardCell.cardView.frame, -320.0, 0.0);
}];
}
Thanks in advance.
It is good if you use the gesture recognizer with the table view.
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeTarget:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[cell addGestureRecognizer:_myTableView];
And your swipe target method like as follows.
- (IBAction)swipeTarget:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint swipePoint = [gestureRecognizer locationInView:_myTableView];
NSIndexPath *cellIndexPath = [_myTableView indexPathForRowAtPoint:swipePoint];
RKCardCell *cell = [_myTableView cellForRowAtIndexPath:cellIndexPath];
// Add your code here to show your view
[UIView animateWithDuration: 0.5 animations:^{
CGRectOffset(cell.cardView.frame, -320.0, 0.0);
}];
}
Take a look on https://github.com/CEWendel/SWTableViewCell .
It's a really good and simple library to make custom swipable tableviewcell.
in my app i have a UICollectionView. i want to apply to it's cells a long pressure gesture. i implemented that but when i run the app only the last cell works and the others do not respond. what it wrong? here is my code.
#interface
#property (nonatomic, strong) UILongPressGestureRecognizer *longPressure;
in view did load:
self.longPressure = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handlePressue:)];
self.longPressure.delegate = self;
[self.collectionView addGestureRecognizer:self.longPressure];
gesture handler:
- (void)handlePressue:(UILongPressGestureRecognizer*)gesture{
[[gesture.view viewWithTag:1] setBackgroundColor:[UIColor yellowColor]];
}
in collectionView:cellforitemAtIndexPAth:
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
imageView.tag = 1;
[imageView setBackgroundColor:[UIColor redColor]];
[cell addGestureRecognizer:self.longPressure];
[cell addSubview:imageView];
return cell;
is there anything wrong?
you need to add gesture on the each cell not for the UICollection View. that might be your problem.the last cell works because the gesture is added to the last cell only.
You need to creat long press gesture for each cell. Because gesture like other ui control,only one in the project.If you want add it to other view,you need copy another one.
Have a look at following Code snippet
MyFlowLayout *myLayout = [[MyFlowLayout alloc]init];
[self.collectionView setCollectionViewLayout:myLayout animated:YES];
UIGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]
}
initWithTarget:self action:#selector(handlePinch:)];
[self.collectionView addGestureRecognizer:pinchRecognizer];
Handling the Pinch:
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)sender {
// Get a reference to the flow layout
MyFlowLayout *layout =
(MyFlowLayout
*)self.collectionView.collectionViewLayout;
// If this is the start of the gesture
if (sender.state == UIGestureRecognizerStateBegan) {
// Get the initial location of the pinch?
CGPoint initialPinchPoint =
[sender locationInView:self.collectionView]; //Convert pinch location into a specific cell
NSIndexPath *pinchedCellPath =
[self.collectionView
indexPathForItemAtPoint:initialPinchPoint];
// Store the indexPath to cell
layout.currentCellPath = pinchedCellPath; }
else if (sender.state == UIGestureRecognizerStateChanged) {
// Store the new center location of the selected cell layout.currentCellCenter =
[sender locationInView:self.collectionView]; // Store the scale value
layout.currentCellScale = sender.scale; }
else {
[self.collectionView performBatchUpdates:^{ layout.currentCellPath = nil;
layout.currentCellScale = 1.0;
} completion:nil];
} }
For performance reasons I suggest you to avoid adding subviews and/or gestures to every single cell. Instead, add a singe longPressureGesture to your collectionView and on the gesture selector handle your per-cell switch business logic. Something as follow:
On your viewDidLoad or where you setup your collection, add the following gesture:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(showDeleteActions:)];
longPress.delegate = self;
[_aCollectionView addGestureRecognizer:longPress];
then on the selector do the single-cell handling:
- (void)showDeleteActions:(UILongPressGestureRecognizer*)gesture {
if (gesture.state == UIGestureRecognizerStateBegan)
{
NSIndexPath *indexPath = [_aCollectionView indexPathForItemAtPoint:[gesture locationInView:_aCollectionView]];
UICollectionViewCell *cell = [_aCollectionView cellForItemAtIndexPath:indexPath];
NSLog(#"cell to delete at IndexPath: %#", indexPath);
}
}
this approach is much more efficient abd far more stable.
Here what it is causing that your UILongPressGestureRecognizer getting overwrite to last cell. So you need to create UILongPressGestureRecognizer for each cell in cellForRowAtIndexPath:
Here is a small code snippet that can help you out
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
imageView.tag = 1;
[imageView setBackgroundColor:[UIColor redColor]];
UILongPressGestureRecognizer *longPressure =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handlePressue:)];
longPressure.delegate = self;
cell.userInteractionEnabled = YES; // Make userInteractionEnabled enable so it can detect touch
[cell addGestureRecognizer:self.longPressure];
[cell addSubview:imageView];
return cell;
There is no need to create any property for the UILongPressGestureRecognizer in .h file. And also remove code to add userInteractionEnabled on whole UICollectionView.